我有这两个类:
<?php
class Test
{
protected $x = 0;
public function setX($x)
{
$this->x = $x;
}
public function getX()
{
echo $this->x;
}
}
class TestEx extends Test
{
//parent::$this->x = 7; it gives this error: Parse error: syntax error, unexpected '$x' (T_VARIABLE), expecting function (T_FUNCTION) in test.php
public function getX()
{
echo parent::$this->x;
}
}
$call_textex = new TestEx();
$call_textex->getX();
?>
现在,我要从继承的类中设置基类的 $x 属性。如何在 PHP5 中实现这一点?