I'm trying to change a property in my parent class with my child class but I'm not getting the result I'm expecting. I've done some research (like Change parent variable from child class), but I can't seem to find the problem.
class A { public $msg; public function __construct() { $this->msg = 'foo'; } public function setMessage($string) { $this->msg = $string; } public function getMessage() { var_dump($this->msg); // For demo purposes } public function triggerB() { $b = new B; } } class B extends A { public function __construct() { parent::setMessage('bar'); } } $a = new A; $a->getMessage(); $a->triggerB(); $a->getMessage(); The output I get is "foo" twice and I expect it to be "foo" "bar".
Could anyone explain me what i'm doing wrong and how I can fix this?
In my actual code I want the child-class to validate some $_POST values, and return the outcome to the Main-class. The parent uses the child to validate.
new B(). Second, you are creating instances, they have no reference to eachother. I guess you are thinking statically, which isn't the purpose of orientating in objects ..