If you have either a private or a protected variable (member) in the parent then you can access it simply like this from you child class: $this->varA = ‘something’ There reason why your child method does not reflect the change, is that child and parent are two different objects in separate memory space. If you want them to share a value you could make it static You don’t need to declare it public class Parent { private $varA; protected $varB; public $varC; protected static $varD; public function getD() { return self::$varD; } public function setD($value) { self::$varD = $value; } } class Child extends Parent { public function getA() { return $this->varA; } public function getB() { return $this->varB; } public function getC() { return $this->varC; } } $child = new Child(); $child->getA(); // Will not work since $varA is private to Parent $child->getB(); // Works fine because $varB is accessible by Parent and subclasses $child->getC(); // Works fine but ... $child->varC; // .. will also work. $child->getD(); // Will work and reflect any changes to the parent or child If you don’t want all instance of the parent class to share values.
You could pass on the parent or child to either new instance and through and update the values of all the related objects accordingly $parent->addChild(new Child()) And in the set method: $this->varA = $value; foreach ($this->children as $child) { $child->setVarA($value); } Hopes this helps.
If you have either a private or a protected variable (member) in the parent then you can access it simply like this from you child class: $this->varA = ‘something’; There reason why your child method does not reflect the change, is that child and parent are two different objects in separate memory space. If you want them to share a value you could make it static. You don’t need to declare it public.
Class Parent { private $varA; protected $varB; public $varC; protected static $varD; public function getD() { return self::$varD; } public function setD($value) { self::$varD = $value; } } class Child extends Parent { public function getA() { return $this->varA; } public function getB() { return $this->varB; } public function getC() { return $this->varC; } } $child = new Child(); $child->getA(); // Will not work since $varA is private to Parent $child->getB(); // Works fine because $varB is accessible by Parent and subclasses $child->getC(); // Works fine but ... $child->varC; // .. will also work. $child->getD(); // Will work and reflect any changes to the parent or child. If you don’t want all instance of the parent class to share values.
You could pass on the parent or child to either new instance and through and update the values of all the related objects accordingly. $parent->addChild(new Child()); And in the set method: $this->varA = $value; foreach ($this->children as $child) { $child->setVarA($value); } Hopes this helps.
Okay, I just tried setting it as protected but I could not view the change directly. I realize that I am creating two different instances of parentClass, but I cannot figure out a way to have 2 variables that target childClass and parentClass. In the sense of the code I wrote above in the question.
– irontombraider Jun 12 at 6:04 If you don’t use static variables and there is no relation between the objects (not the classes) whatsoever you cannot achieve this. You have to either make the variable static (getD() in my example) - shared by all instances or like I suggested (after editing) setting up relations between the objects. – Michael Jun 12 at 6:08.