RE: Parent Object

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




> -----Original Message-----
> From: Ralph Kutschera [mailto:ecuapac@xxxxxx]
> Sent: 01 August 2007 13:19
> To: php-general@xxxxxxxxxxxxx
> Subject:  Parent Object
>
>
> Hallo!
>
> class A {
>   ...
> }
>
> class B extends A {
>   ...
>   $name = get_parent_class($this); // would be "A"
>   ...
> }
>
> This is *not* what I want!
>
> class B {
>   ...
>   $name = get_parent_class2($this); // should give "A"
>   ...
> }
>
> class A {
>   ...
>   $b = new B();
>   ...
> }
>
> This *is* what I want!
>
> How can i resolve this issue? I don't want to know which class is being
> inherited, but I'd like to know which class has created the
> current object.
>
> TIA, Ralph

The concept of a parent class only exists with inheritance. In terms of
finding where an instance is created from this is non-sensical. Who's to say
the instance is even created by another class? It could be in a function.

If you want to be able to access an object that created another object, pass
it in via the constructor, e.g.

class A {
   ...
   $b = new B($this);
   ...
}

class B {
   private $a;

   public function __construct($a) {
      $this->a = $a;
   }
}

Edward

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux