【问题标题】:PHP inheritance: child class overriding parent variable/property for use in constructorPHP继承:子类覆盖父变量/属性以在构造函数中使用
【发布时间】:2013-08-01 15:46:00
【问题描述】:

我有一个(抽象的)父类应该在构建期间提供功能。子类可以覆盖构造函数中使用的属性:

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;
}

问题是在运行未被覆盖的Child::__construct() 时不使用被覆盖的属性($this->createChannel 使用NULL 参数调用)。

这在 PHP 中是否可行,还是我每次都必须求助于覆盖子构造函数来提供所需的功能?

注意:我看到了Properties shared between child and parents class in php,但这是不同的,因为子属性不是在构造函数中分配的,而是根据定义分配的。

更新

原来我的测试用例有问题。由于 MiddlewareTest 基于 SimpleTest 单元测试用例,SimpleTest 实际上——我没有意识到——通过它的自动运行实例化了从未有意的 Parent 类本身。通过使父类抽象来修复。

经验教训:构建一个干净的测试用例并在寻求帮助之前实际运行它。

【问题讨论】:

    标签: php inheritance simpletest


    【解决方案1】:

    我不确定您的服务器上是如何发生的。我不得不对 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?

    【讨论】:

    • 很抱歉没有提供一个工作示例。你的提示让我开始建造一个,我明白你的意思。现在正试图找出不同之处。
    猜你喜欢
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    相关资源
    最近更新 更多