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! Link to comment https://forums.phpfreaks.com/topic/77422-solved-php4-oop-newbie-question-pointer-to-parent-object/ 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??? Link to comment https://forums.phpfreaks.com/topic/77422-solved-php4-oop-newbie-question-pointer-to-parent-object/#findComment-392168 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 } Link to comment https://forums.phpfreaks.com/topic/77422-solved-php4-oop-newbie-question-pointer-to-parent-object/#findComment-392176 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; } } Link to comment https://forums.phpfreaks.com/topic/77422-solved-php4-oop-newbie-question-pointer-to-parent-object/#findComment-392199 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. Link to comment https://forums.phpfreaks.com/topic/77422-solved-php4-oop-newbie-question-pointer-to-parent-object/#findComment-392206 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. Link to comment https://forums.phpfreaks.com/topic/77422-solved-php4-oop-newbie-question-pointer-to-parent-object/#findComment-392214 Share on other sites More sharing options...
Raj Singh Posted November 15, 2007 Author Share Posted November 15, 2007 Ok, I understand Link to comment https://forums.phpfreaks.com/topic/77422-solved-php4-oop-newbie-question-pointer-to-parent-object/#findComment-392229 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.