4

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.

4

1 回答 1

2

我不确定您的服务器上是如何发生的。我不得不对MiddlewareTest类做出假设,修改你的类名,并添加一些简单的调试行,但是使用以下代码:

<?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();
?>

我得到这些结果:

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,
)'

正如您所看到的,这些值最终被传入,就像它们在实例化类中定义的一样,正如预期的那样。

您能否提供有关 MiddlewareTest 类的一些详细信息,以说明您可能会遇到这种行为的原因?

你运行的是什么版本的php?

于 2013-08-01T16:48:30.007 回答