静态方法- PHP中的多个paamayim nekudotayim,为什么不呢?


static methods - Multiple paamayim nekudotayims in PHP, why not?

PHP 5.3.6中,我注意到以下内容不起作用:

class Foo{
    public static $class = 'Bar';
}
class Bar{
    public static function sayHello(){
        echo 'Hello World';
    }
}
Foo::$class::sayHello();

签发unexpected T_PAAMAYIM_NEKUDOTAYIM。然而,使用临时变量会产生预期的结果:

$class = Foo::$class;
$class::sayHello(); // Hello World

有谁知道这是设计的,还是范围解析操作符被标记或其他东西的意外结果?有没有比后一个临时变量示例更简洁的解决方法?

不幸的是,没有办法在一行中完成。我以为你可以用call_user_func(),但没有去:

call_user_func(Foo::$class.'::sayHello()');
// Warning: call_user_func() expects parameter 1 to be a valid callback, class 'Bar' does not have a method 'sayHello()'

另外,你为什么要做这样的事情呢?我相信一定有更好的方法来做你想做的事情-通常有,如果你使用变量变量或类名。