PHP Inheritance: child class overrides parent variable / property for use in constructor

I have a (abstract) parent class that should provide functionality during construction. Classes for children can override the properties used in the constructor:

class Parent extends MiddlewareTest { // abstract channel properties protected $title = NULL; protected $type = NULL; protected $resolution = NULL; function __construct() { parent::__construct(); $this->uuid = $this->createChannel($this->title, $this->type, $this->resolution); } } class Child extends Parent { // channel properties protected $title = 'Power'; protected $type = 'power'; protected $resolution = 1000; } 

The problem is that overridden properties are not used when Child::__construct() , which is not an overridden run ( $this->createChannel is called with NULL parameters).

Is this possible in PHP or will I have to resort to overriding child constructors every time to provide the desired functions?

Note. I saw Properties shared between the child and parent class in php , but this is different since the child properties are not assigned in the constructor, but by definition.

Update

It turned out that my test file was faulty. Since MiddlewareTest was based on the SimpleTest unit test case, SimpleTest, in fact, what I did not understand, is an autorun created by the Parent class itself, which was never conceived. Fixed using the abstract class of the parent.

Lessons learned: Create a clean test case and actually run it before crying for help.

+4
source share
1 answer

I am not sure how this happens on your server. I had to make assumptions about the MiddlewareTest class, change the class names and add some simple debugging lines, but with this code:

 <?php /** * I'm not sure what you have in this class. * Perhaps the problem lies here on your side. * Is this constructor doing something to nullify those properties? * Are those properties also defined in this class? */ abstract class MiddlewareTest { // I assume this properties are also defined here protected $title = NULL; protected $type = NULL; protected $resolution = NULL; protected $uuid = NULL; public function __construct() {} protected function createChannel($title, $type, $resolution) { echo "<pre>" . __LINE__ . ": "; var_export(array($this->title, $this->type, $this->resolution)); echo "</pre>"; echo "<pre>" . __LINE__ . ": "; var_export(array($title, $type, $resolution)); echo "</pre>"; return var_export(array($title, $type, $resolution), true); } } // 'parent' is a keyword, so let just use A and B class A extends MiddlewareTest { // abstract channel properties protected $title = NULL; protected $type = NULL; protected $resolution = NULL; function __construct() { parent::__construct(); echo "<pre>" . __LINE__ . ": "; var_export(array($this->title, $this->type, $this->resolution)); echo "</pre>"; $this->uuid = $this->createChannel($this->title, $this->type, $this->resolution); echo "<pre>" . __LINE__ . ": "; var_export($this->uuid); echo "</pre>"; } } class B extends A { // channel properties protected $title = "Power"; protected $type = "power"; protected $resolution = 1000; } $B = new B(); ?> 

I get the following results:

 37: array ( 0 => 'Power', 1 => 'power', 2 => 1000, ) 20: array ( 0 => 'Power', 1 => 'power', 2 => 1000, ) 21: array ( 0 => 'Power', 1 => 'power', 2 => 1000, ) 39: 'array ( 0 => \'Power\', 1 => \'power\', 2 => 1000, )' 

As you can see, the values ​​appear to be passed in the same way as they are defined in the instance class, as expected.

Can you give some information about your MiddlewareTest class, which may shed light on why you might experience this behavior?

What version of php are you using?

+2
source

Source: https://habr.com/ru/post/1494775/


All Articles