【发布时间】: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