Jump to content

Object inheritance, not keeping property


optikalefx

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

  • 2 weeks later...

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"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.