Raj Singh Posted November 15, 2007 Share Posted November 15, 2007 let's say I have class A and class B I create B object inside an A object. Is that possible to have a pre-defined pointer to the parent A object inside the B object? thank you! Quote Link to comment Share on other sites More sharing options...
aschk Posted November 15, 2007 Share Posted November 15, 2007 Maybe some example code of what you mean might help define this problem better. Here's my attempt at examplifying what you described // Class A class A { private $internalVar = "string1"; // Get internal Var. public function getInternalVar(){ return $this->internalVar; } } // Class B class B { // Private variable of type A private $aObject; // Set the private internal variable to an instanceof A. public function setAObject(A $a){ $this->aObject = $a; } // Method call to A::getInternalVar() public function getInternalVar(){ return $this->aObject->getInternalVar(); } } $classA = new A(); $classB = new B(); $classB->setAObject($a); echo $classB->getInternalVar(); Is that what you meant? Or you really talking about inheritance??? Quote Link to comment Share on other sites More sharing options...
Raj Singh Posted November 15, 2007 Author Share Posted November 15, 2007 Thank you for helping me. First, I'm using PHP 4 but it might not be an issue. Here's what I had in mind $Foo = new Foo(); class Foo { var $MyObject; function Foo { $this->MyObject = new Bar(); } } class Bar { var $PointerToObjectFoo;//Here I want this variable to be a pointer to the $Foo Object of the Main } Quote Link to comment Share on other sites More sharing options...
aschk Posted November 15, 2007 Share Posted November 15, 2007 Yes it's do-able. I recommend changing to PHP5 because otherwise you're going to have issues with objects/classes NOT being references like they are in 5. $Foo = new Foo(); class Foo { var $MyObject; function Foo { $this->MyObject =& new Bar($this); } } class Bar { var $PointerToObjectFoo;//Here I want this variable to be a pointer to the $Foo Object of the Main function Bar($foo){ // Create reference to Object of type Foo $this->PointerToObjectFoo =& $foo; } } Quote Link to comment Share on other sites More sharing options...
Raj Singh Posted November 15, 2007 Author Share Posted November 15, 2007 OK thank you! I thought there was a predefined variable for it. Quote Link to comment Share on other sites More sharing options...
aschk Posted November 15, 2007 Share Posted November 15, 2007 If Bar extended Foo, then yes there is, but there you have two DIFFERENT classes, so without creating a giving the created Bar object a reference to the Foo object Foo will never know what created it. Quote Link to comment Share on other sites More sharing options...
Raj Singh Posted November 15, 2007 Author Share Posted November 15, 2007 Ok, I understand Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.