bulletseed Posted February 28, 2007 Share Posted February 28, 2007 I'm kinda new to PHP but usually I can find anything on the documentation or forums. Yet this question has eluded me. Is there any php function to get an object's parent object? Right now, the only way i can think of is to send it as a parameter to a method. But I was just wondering if there was a built-in way to do this. Link to comment https://forums.phpfreaks.com/topic/40547-get-parent-object/ Share on other sites More sharing options...
obsidian Posted February 28, 2007 Share Posted February 28, 2007 Have you tried using the get_parent_class() method? Link to comment https://forums.phpfreaks.com/topic/40547-get-parent-object/#findComment-196207 Share on other sites More sharing options...
bulletseed Posted February 28, 2007 Author Share Posted February 28, 2007 yeah i have. i got excited when i saw that function in the docs but unfortunately it just returns the string name of the class. i need the actual parent object to check some variables inside it. Link to comment https://forums.phpfreaks.com/topic/40547-get-parent-object/#findComment-196210 Share on other sites More sharing options...
obsidian Posted February 28, 2007 Share Posted February 28, 2007 yeah i have. i got excited when i saw that function in the docs but unfortunately it just returns the string name of the class. i need the actual parent object to check some variables inside it. I'm not sure I understand what you're after. Parent object refers to the object that the child class extends. I'm not positive this is how you're using it. It sounds almost like you have an object as a variable of another object. Can you just try to be a little more descriptive of your situation? Link to comment https://forums.phpfreaks.com/topic/40547-get-parent-object/#findComment-196213 Share on other sites More sharing options...
bulletseed Posted February 28, 2007 Author Share Posted February 28, 2007 yeah, i guess "parent object" is not what i really meant. this is literally what i mean: i have one instance of a class (lets call it classParent) which has its member variables etc. and also has an array that contains a few instances of another class (lets call that one classChild). so the thing is that i need to be able to access the values of the variables on classParent from any given instance of classChild. I will write some code here to better explain myself: class classParent { $variable1; $children; } class classChild { public function whatever() { echo <insert-idea-here-to-get-parent's-$variable1>; } } $p = new classParent(); $c = new classChild(); $p->children[] = $c; $p->children[0]->whatever(); There. I need something where it says <insert-idea-here-to-get-parent's-$variable1> without using 'extends' in the child class. As far as i know, the only way i can find to do this is to send the parent object as a parameter to the method, like this: $p->children[0]->whatever($p); ...and then having the method contain... echo $p->variable1; Link to comment https://forums.phpfreaks.com/topic/40547-get-parent-object/#findComment-196237 Share on other sites More sharing options...
itsmeArry Posted February 28, 2007 Share Posted February 28, 2007 you can use parent::variablename or parent::functionname but it won't access private members.. Link to comment https://forums.phpfreaks.com/topic/40547-get-parent-object/#findComment-196263 Share on other sites More sharing options...
obsidian Posted February 28, 2007 Share Posted February 28, 2007 you can use parent::variablename or parent::functionname but it won't access private members.. Again, though, this solution only works if your "child" class extends your parent class. Without having some sort of OOP communication in that way, you have to at least send a reference to the "parent" object through the function call. Link to comment https://forums.phpfreaks.com/topic/40547-get-parent-object/#findComment-196315 Share on other sites More sharing options...
bulletseed Posted February 28, 2007 Author Share Posted February 28, 2007 ok, thats what i needed to hear. thanks! Link to comment https://forums.phpfreaks.com/topic/40547-get-parent-object/#findComment-196318 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.