我试图在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的值,则会得到该错误。我也不知道原因?
发布于 2016-09-23 04:08:53
要将值赋值给作为表达式结果的静态变量,将导致解析错误。
static $int = 0; // correct
static $int = 1+2; // wrong (as it is an expression)
static $int = sqrt(121); // wrong (as it is an expression too)
https://stackoverflow.com/questions/39652042
复制相似问题