bulletseed Posted February 23, 2007 Share Posted February 23, 2007 Hi. I'm kinda new at extending classes. I have tried searching all over this and other forums about this problem and I just haven't been able to figure it out. I have one class extending another and I'm trying to modify the value of a variable in the parent class. The problem is that once the function in the child class ends, the parent's variable that I just changed turns back into what it was before. <?php class _parent { public $var; public function __construct() { $this->var = 'OLD'; } public function show() { $child = new _child(); $child->change(); echo $this->var; } } class _child extends _parent { public function change() { $this->var = 'NEW'; } } $parent = new _parent(); $parent->show(); /* Why is output not NEW ??? */ ?> What am I doing wrong? Or am I trying something that is not possible? Thanks for your feedback. -BulletSeed Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 23, 2007 Share Posted February 23, 2007 You never call the function $parent->change() Quote Link to comment Share on other sites More sharing options...
bulletseed Posted February 23, 2007 Author Share Posted February 23, 2007 Huh? But there is no change() function in the parent class. There is a change() function in the child class which I am calling from within the parent's show() function. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 23, 2007 Share Posted February 23, 2007 Ah right. You create a new child. The variable that gets changed is the one in child, not the parent. If you want to change $parent's variable, you need to pass $parent to the function or have it return a value which you can then assign. You have like this: $a is parent. $b is a variable inside $a; make a change to $b, it doesn't affect $a. Why do all your classes start with _? that's going to get annoying to type all the time. Quote Link to comment Share on other sites More sharing options...
bulletseed Posted February 23, 2007 Author Share Posted February 23, 2007 (The underscore is just there because in this example i wanted to use the 'parent' and 'child' names for the classes for clarity but parent is a reserved word in PHP. I don't use them in my real project.) Well the thing is that i cant return that value in that function because in my real project I'm already returning something else there. I just wanted to know if what I am trying to do here (modifying a variable in a parent class from WITHIN a child class) was actually possible at all. I thought it would be. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 23, 2007 Share Posted February 23, 2007 No because what you end up modifying is the $child, not the $parent Quote Link to comment Share on other sites More sharing options...
bulletseed Posted February 23, 2007 Author Share Posted February 23, 2007 but... but... but... it EXTENDS! isnt that what that keyword is supposed to be for? :'( Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 23, 2007 Share Posted February 23, 2007 No, extends means the class includes all the variables and functions of the parent. It doesn't mean the $child is a pointer to $parent, which is how you're trying to use it, as if $child was $parent, so any changes you make to $child would apply to $parent. That's not what extends means. Quote Link to comment Share on other sites More sharing options...
bulletseed Posted February 23, 2007 Author Share Posted February 23, 2007 ahh, gotcha. hmmm... if that's how it works.. then... ...ok... i just tried doing the same thing by declaring the $var as static and referencing it with the '::' operator on both classes and it seems to work exactly like I wanted originally. I guess that's the correct way of doing what I was thinking. 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.