【发布时间】:2017-01-31 19:10:22
【问题描述】:
我正在尝试在 PHP 中执行此代码:
class T {
public $y = 4;
public function y() { return $this->y; }
public function q()
{
static $j = $this->y;
echo $j;
}
}
$r = new T();
$r->q();
我收到以下错误:
Fatal error: Constant expression contains invalid operations in C:\xampp\htdocs\dermaquality\test.php on line 13
static $j = $this->y;
如果我手动设置该值,则没有问题,但如果我设置调用 y() 或 $this->y 的值,我会收到该错误。 不知道为什么?
【问题讨论】:
-
静态属性不能通过使用箭头操作符->的对象访问。 php.net/manual/en/language.oop5.static.php
标签: php class variables methods static