0

__callStatic()关于PHP 的快速问题;

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();

但我试图避免这种情况。

4

3 回答 3

1

您可以通过call_user_func ()调用静态函数。

于 2011-07-19T07:46:05.747 回答
1

我认为 PHP 解析器目前无法处理。我现在无法证明它,但我认为这是一个类似的问题,就像函数调用之后的数组取消引用问题(callme()['arraykey'])。

于 2011-07-19T07:21:25.480 回答
0

这已在 PHP 5.4 中解决

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)

https://svn.php.net/repository/php/php-src/tags/php_5_4_0RC8/NEWS

于 2012-02-17T04:53:53.920 回答