Question

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.

Was it helpful?

Solution

I'm not sure how that's happening on your server. I had to make assumptions about the MiddlewareTest class, modify your class names, and added 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's 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 these 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 turn out to be passed in just as they are defined in the instantiated class, just as expected.

Can you give some details on your MiddlewareTest class that might shed some light on why you might be experiencing this behavior?

What version of php are you running?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top