RE: [PHP] Parent Object

From: Date: Wed, 01 Aug 2007 12:34:58 +0000
Subject: RE: [PHP] Parent Object
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-259984@lists.php.net to get a copy of this message
> -----Original Message----- > From: Ralph Kutschera [mailto:ecuapac@gmx.at] > Sent: 01 August 2007 13:19 > To: php-general@lists.php.net > Subject: [PHP] 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

« previous php.general (#259984) next »