为什么可以覆盖在父 class 中使用的特征中定义的 属性 的初始值?
Why can you override the initial value of a property defined in a trait that is used in a parent class?
PHP 文档说明了以下关于重写特征属性的内容:
If a trait defines a property then a class can not define a property
with the same name unless it is compatible (same visibility and
initial value), otherwise a fatal error is issued.
但是,当您在抽象 class 中使用特征时,您可以覆盖 class 中特征中定义的属性,扩展该抽象 class:
<?php
trait PropertyTrait
{
public $prop = 'default';
}
abstract class A
{
use PropertyTrait;
}
class B extends A
{
public $prop = 'overridden';
public function write()
{
echo $this->prop;
}
}
$b = new B();
$b->write(); // outputs "overridden"
上面的代码有效,但我在文档中找不到任何关于它的参考。这是预期的功能吗?
PHP 文档说明了以下关于重写特征属性的内容:
If a trait defines a property then a class can not define a property with the same name unless it is compatible (same visibility and initial value), otherwise a fatal error is issued.
但是,当您在抽象 class 中使用特征时,您可以覆盖 class 中特征中定义的属性,扩展该抽象 class:
<?php
trait PropertyTrait
{
public $prop = 'default';
}
abstract class A
{
use PropertyTrait;
}
class B extends A
{
public $prop = 'overridden';
public function write()
{
echo $this->prop;
}
}
$b = new B();
$b->write(); // outputs "overridden"
上面的代码有效,但我在文档中找不到任何关于它的参考。这是预期的功能吗?