2

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.

1
  • First of all, if you wish to call a constructor, use paranthesis, e.g. 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 .. Commented Sep 29, 2016 at 15:26

2 Answers 2

3

You are having your object A and creating an instance of it and storing it in the variable $a, in the global scope. And then you are creating another instance of your class B and storing it in a variable $b which is in the scope of the method triggerB().

You can only change the properties of the parent class A if you pass an argument to your another class B.

So something like this should suffice:

<?php declare(strict_types = 1); class A { public $msg; public function __construct() { $this->msg = 'foo'; } public function setMessage(string $string) { $this->msg = $string; } public function getMessage() { var_dump($this->msg); // For demo purposes } public function triggerB() { (new B($this)); } } class B { public function __construct(A $a) { $a->msg = "bar"; } } $a = new A; $a->getMessage(); $a->triggerB(); $a->getMessage(); 

This approach is better suited to readability and better dependency management.


Another approach:

<?php declare(strict_types = 1); class A { public $msg; public function __construct() { $this->msg = 'foo'; } public function setMessage(string $string) { $this->msg = $string; } public function getMessage() { var_dump($this->msg); // For demo purposes } public function triggerB() { $this->msg = 'bar'; } } class B { public function __construct(A $a) { $a->msg = "bar"; } } $a = new A; $a->getMessage(); $a->triggerB(); $a->getMessage(); 

This is performance wise better, but if you are going to be doing something complex, the first method is better.


Note: The above code is for PHP7.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, How is this performance-wise(eg what if class is big)? And is this a good practice? It seems odd.
The best possible way would be to set the property of the class in the same method as triggerB() like - public function triggerB() { $this->msg = 'Bar'; } but if you are going to do something complex and some processing, the readability of code should matter the most than performance, and this approach would be better suited.
1

Your triggerB() method does not actually do anything:

public function triggerB() { $b = new B; } 

You are creating a new object and assign that to the $b variable. As soon as the method finishes, the $b variable / object ceases to exist.

Also note that the $b variable in your method is is no way related to the $a variable in the global scope so setting any of its properties has no influence on $a.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.