关于PHP中__callStatic()
的快速问题;
class Test{
public static function __callStatic($method, $arguments){
echo $method . PHP_EOL;
}
public function __call($method, $arguments){
echo $method . PHP_EOL;
}
}
$test = new Test();
$test->foo();
$test->{'hello-world'}();
Test::bar();
Test::{'goodbye-universe'}();
预期输出:foo
hello-world
bar
goodbye-universe
实际输出:foo
hello-world
bar
PHP Parse error: syntax error, unexpected '{', expecting T_STRING or T_VARIABLE or '$' in - on line 18
这样的语法是不允许的吗?使用__callStatic()
是否可以实现这样的功能?
注意:试图摆脱没有临时变量。下面的命令可以工作:
$goodbyeUniverse = 'goodbye-universe';
Test::$goodbyeUniverse();
但是我尽量避免。
我认为PHP解析器目前无法处理这个问题。我现在不能证明,但我认为这是一个类似的问题,就像函数调用(callme()['arraykey']
)后的数组解引用问题。
可以通过call_user_func()调用静态函数
这个问题已经在PHP 5.4中解决了
https://svn.php.net/repository/php/php-src/tags/php_5_4_0RC8/NEWS
04 Aug 2011, PHP 5.4.0 Alpha 3
- Added features:
. Short array syntax, see UPGRADING guide for full details
(rsky0711 at gmail . com, sebastian.deutsch at 9elements . com, Pierre)
. Binary numbers format (0b001010). (Jonah dot Harris at gmail dot com)
. Support for Class::{expr}() syntax (Pierrick)