PHP __callStatic &方法名称字符无效
关于 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();
但我试图避免这种情况。
Quick question regarding __callStatic()
in 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'}();
Expected output:
foo
hello-world
bar
goodbye-universe
Actual output:
foo
hello-world
bar
PHP Parse error: syntax error, unexpected '{', expecting T_STRING or T_VARIABLE or '
Is this syntax not permissible, nor functionality achievable with __callStatic()
?
Note: Trying to get away with no temporary variables. The following will work:
$goodbyeUniverse = 'goodbye-universe';
Test::$goodbyeUniverse();
But I'm trying to avoid that.
in - on line 18
Is this syntax not permissible, nor functionality achievable with __callStatic()
?
Note: Trying to get away with no temporary variables. The following will work:
But I'm trying to avoid that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 PHP 解析器目前无法处理这个问题。我现在无法证明这一点,但我认为这是一个类似的问题,例如函数调用后的数组取消引用问题(
callme()['arraykey']
)。I don't think the PHP parser can handle that currently. I cannot prove it just now, but I think it's a similar issue like the array-dereferencing-issue after function calls (
callme()['arraykey']
).您可以通过 call_user_func() 调用静态函数。
you could invoke a static function via call_user_func().
PHP 5.4 中已解决此问题
This has been resolved in PHP 5.4
https://svn.php.net/repository/php/php-src/tags/php_5_4_0RC8/NEWS