optikalefx Posted June 7, 2010 Share Posted June 7, 2010 I know the problem must be dumb, but this is killing me. I have 2 classes, one extends another one. The base class has a variable and later in the code the base class changes that variable. But after i instantiate the extended class it only has the original value. echo $this->myvar; // start $this->myvar = "test "; echo $this->myvar; // test require_once("controllers/blog.controller.php"); $blog = new Blog(); echo $blog->myvar; // start ??????? here is app.controller.php class App { var $myvar = "start "; } and here is blog.controller.php class Blog extends App { } that variable $this->myvar should be the same all the time right? ahhhhhhhhhhh Quote Link to comment https://forums.phpfreaks.com/topic/204059-object-inheritance-not-keeping-property/ Share on other sites More sharing options...
trq Posted June 7, 2010 Share Posted June 7, 2010 The two instances of the objects aren't related, only there base classes. Quote Link to comment https://forums.phpfreaks.com/topic/204059-object-inheritance-not-keeping-property/#findComment-1068817 Share on other sites More sharing options...
optikalefx Posted June 7, 2010 Author Share Posted June 7, 2010 ok so i wrote that wrong. here is a full php file <?php class base { var $myvar = "start"; function changeValue() { $this->myvar = "changed"; } } class extender extends base { function showMyvar() { echo $this->myvar; } } class extender2 extends base { function extender2() { $this->myvar = "2 changed"; } } $test = new extender; $test2 = new extender2; // should change the value echo $test->myvar; // prints as start ?> why can't i change the base class variable? Quote Link to comment https://forums.phpfreaks.com/topic/204059-object-inheritance-not-keeping-property/#findComment-1068819 Share on other sites More sharing options...
trq Posted June 7, 2010 Share Posted June 7, 2010 They are two completely different instances. Quote Link to comment https://forums.phpfreaks.com/topic/204059-object-inheritance-not-keeping-property/#findComment-1068835 Share on other sites More sharing options...
optikalefx Posted June 8, 2010 Author Share Posted June 8, 2010 Ok i think i got it. Once you make a variable in the parent class, its subclasses can't change it. Quote Link to comment https://forums.phpfreaks.com/topic/204059-object-inheritance-not-keeping-property/#findComment-1069260 Share on other sites More sharing options...
jcbones Posted June 8, 2010 Share Posted June 8, 2010 Try: $test->changeValue(); echo $test2->myvar . '<br />' . $test->myvar; Quote Link to comment https://forums.phpfreaks.com/topic/204059-object-inheritance-not-keeping-property/#findComment-1069274 Share on other sites More sharing options...
KevinM1 Posted June 8, 2010 Share Posted June 8, 2010 Ok i think i got it. Once you make a variable in the parent class, its subclasses can't change it. Nope, still wrong. Try: class Base { protected $baseVar = "Start"; public function show() { echo $this->baseVar; } } class Child extends Base { public function edit($value) { $this->baseVar = $value; // Child has access to Base::baseVar because it inherits from Base } } $test = new Child(); $test->show(); // should display 'Start' $test->edit("I'm changing my innards! WEEEEE!"); $test->show(); Quote Link to comment https://forums.phpfreaks.com/topic/204059-object-inheritance-not-keeping-property/#findComment-1069455 Share on other sites More sharing options...
optikalefx Posted June 23, 2010 Author Share Posted June 23, 2010 I much better understand inheritance now. My original thought was that the parent class had properties that all any sub class could change, as in the subclass is changing the parent's property. But that is not inheritance. That is just nonsense. The parent class defines the variables that may exist in the subclass. And every subclass has INSTANCES of the parent's properties, because the child class inherit those properties. So if i have a a property in the parent class called $this->myvar. Sub class 1 sets $this->myvar to "hello" When subclass 2 gets instantiated, it is a NEW copy of the base class, and therefore the value of $this->myvar is not "hello" it is simply blank. Upon changing $this->myvar in subclass 2, subclass 1 is STILL "hello" Quote Link to comment https://forums.phpfreaks.com/topic/204059-object-inheritance-not-keeping-property/#findComment-1075964 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.