PHP 中的多个 paamayim nekudotayims,为什么不呢?
在 PHP 5.3.6 中,我注意到以下操作不起作用:
class Foo{
public static $class = 'Bar';
}
class Bar{
public static function sayHello(){
echo 'Hello World';
}
}
Foo::$class::sayHello();
发出意外的 T_PAAMAYIM_NEKUDOTAYIM
。但是,使用临时变量会产生预期的结果:
$class = Foo::$class;
$class::sayHello(); // Hello World
有谁知道这是设计使然,还是范围解析运算符标记化的意外结果或其他结果?有比后一个临时变量示例更干净的解决方法吗?
In PHP 5.3.6, I've noticed that the following won't work:
class Foo{
public static $class = 'Bar';
}
class Bar{
public static function sayHello(){
echo 'Hello World';
}
}
Foo::$class::sayHello();
Issuing an unexpected T_PAAMAYIM_NEKUDOTAYIM
. Using a temporary variable however, results in the expected:
$class = Foo::$class;
$class::sayHello(); // Hello World
Does anyone know if this is by design, or an unintended result of how the scope resolution operator is tokenized or something? Any cleaner workarounds than the latter, temporary variable example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,没有办法在一行中完成它。我以为你可以用 call_user_func() 来做到这一点,但不行:
另外,为什么你首先要做这样的事情呢?我确信一定有更好的方法来完成您想要做的事情 - 如果您使用变量或类名,通常会有更好的方法。
Unfortunately there is no way to do it in one line. I thought you might be able to do it with call_user_func(), but no go:
Also, why would you want to do something like this in the first place? I'm sure there must be a better way to do what you're trying to do - there usually is if you're using variable variables or class names.