Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在匿名函数中使用static::method()会在PHP版本之间产生不同的结果_Php_Oop_Scope - Fatal编程技术网

在匿名函数中使用static::method()会在PHP版本之间产生不同的结果

在匿名函数中使用static::method()会在PHP版本之间产生不同的结果,php,oop,scope,Php,Oop,Scope,在PHP5.3.3上,下面的代码将失败,并出现以下错误 当没有活动的类作用域时,无法访问static:: 我理解这是因为匿名函数创建自己的作用域,不在类的作用域内。 那么是什么让它在PHP5.5.0中工作呢?它在PHP5.4中也可以工作吗?PHP5.3不显式调用PathController::do\u something() 对于公共方法,解决方法基本上与PHP 5.3中的$this范围相同: $class = get_called_class(); $that = $this; retur

在PHP5.3.3上,下面的代码将失败,并出现以下错误

当没有活动的类作用域时,无法访问static::

我理解这是因为匿名函数创建自己的作用域,不在类的作用域内。 那么是什么让它在PHP5.5.0中工作呢?它在PHP5.4中也可以工作吗?PHP5.3不显式调用
PathController::do\u something()


对于公共方法,解决方法基本上与PHP 5.3中的
$this
范围相同:

$class = get_called_class();
$that  = $this;

return Response::json(..., function () use ($class, $that) {
   ...
   call_user_func(array($class, 'do_something_static'), $type);
   $that->do_something_non_static($type);
   ...
});

这不适用于非公共方法,也没有真正优雅的解决方法。

IIRC PHP 5.4引入了匿名函数的范围继承。是
::做些什么
公开的
还是受保护的
?解决方法很大程度上取决于这个标准。@deceze这是一个公共方法谢谢。我考虑过你的解决办法。你会推荐
get\u called\u class()
而不是
\uu class\uuu
吗?如果你想要
静态
行为,你需要
get\u called\u class
<代码>\uuuu类\uuuuuuuuuuuuuuu将为您提供
self
的行为。
$class = get_called_class();
$that  = $this;

return Response::json(..., function () use ($class, $that) {
   ...
   call_user_func(array($class, 'do_something_static'), $type);
   $that->do_something_non_static($type);
   ...
});