【发布时间】:2014-04-01 22:06:13
【问题描述】:
我正在尝试访问静态类变量定义中的静态类方法。我尝试了几次,但无法编译代码。
天真的尝试:
<?php
class TestClass {
private static $VAR = doSomething(array());
private static function doSomething($input) {
return null;
}
}
?>
错误:
dev@box:~/php$ php -l TestClass.php
PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' in TestClass.php on line 3
Errors parsing TestClass.php
直观的尝试:
<?php
class TestClass {
private static $VAR = self::doSomething(array());
private static function doSomething($input) {
return null;
}
}
?>
错误:
dev@box:~/php$ php -l TestClass.php
PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' in TestClass.php on line 3
Errors parsing TestClass.php
逻辑尝试:
<?php
class TestClass {
private static $VAR = static::doSomething(array());
private static function doSomething($input) {
return null;
}
}
?>
错误:
dev@box:~/php$ php -l TestClass.php
PHP Fatal error: "static::" is not allowed in compile-time constants in TestClass.php on line 3
Errors parsing TestClass.php
绝望的尝试:
<?php
class TestClass {
private static $VAR = $this->doSomething(array());
private static function doSomething($input) {
return null;
}
}
?>
错误:
dev@Dev08:~/php$ php -l TestClass.php
PHP Parse error: syntax error, unexpected '$this' (T_VARIABLE) in TestClass.php on line 3
Errors parsing TestClass.php
任务看起来很简单,使用静态方法来定义静态变量,但是我想不通在静态变量声明的上下文中如何正确访问静态方法。
如何在PHP v5.5.3的静态变量定义中调用静态方法?
【问题讨论】:
标签: php class static-methods static-variables