I have an (abstract) parent class supposed to provide functionality during construction. Child classes can override 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;
}
Problem is that the overridden properties are not used when Child::__construct()
which is not overridden runs ($this->createChannel
is called with NULL
parameters).
Is this possible in PHP or will I have to resort to overriding child constructors each time to provide the desired functionality?
Note: I saw Properties shared between child and parents class in php but this is different as the child properties are not assigned in the constructor but by definition.
Update
It turns out my test case was faulty. As the MiddlewareTest was based on SimpleTest unit test case, SimpleTest had actually- what I didn't realize- by it's autorun instantiated the Parent class itself which was never intented. Fixed by making the Parent class abstract.
Lessons learned: build a clean test case and actually run it before crying for help.