PHP 静态匿名函数真的有效吗? [英] Does a PHP static anonymous function really work?
问题描述
我正在尝试学习 PHP,但现在我被困在静态匿名函数"中.
I'm trying to learn PHP, and now I'm stuck in 'static anonymous function'.
我在教程中找到了这个(http://www.slideshare.net/melechi/php-53-part-2-lambda-functions-closures-presentation)
I found this in a tutorial (http://www.slideshare.net/melechi/php-53-part-2-lambda-functions-closures-presentation)
面向对象
- Lambda 函数是闭包,因为它们会自动绑定到创建它们的类的作用域.
- '
$this
' 在作用域中并不总是需要的. - 删除
$this
"可以节省内存. - 您可以通过将 Lambda 函数声明为静态来阻止这种行为."
- Lambda Functions are Closures because they automatically get bound to the scope of the class that they are created in.
- '
$this
' is not always needed in the scope. - Removing '
$this
' can save on memory. - You can block this behaviour by declaring the Lambda Function as static."
这段代码有什么问题?
我收到此错误:
解析错误:解析错误,在第 11 行的 C:\wamp\www\z-final\a.php 中期待`T_PAAMAYIM_NEKUDOTAYIM'
Parse error: parse error, expecting `T_PAAMAYIM_NEKUDOTAYIM' in C:\wamp\www\z-final\a.php on line 11
为什么此代码行不起作用return static function(){var_dump($this);};"?
Why this code line doesn't work "return static function(){var_dump($this);};" ?
class foo
{
public function getLambda()
{
return function(){var_dump($this);};
}
public function getStaticLambda()
{
return static function(){var_dump($this);};
}
}
$foo = new foo();
$lambda = $foo->getLambda();
$staticLambda = $foo->getStaticLambda();
$lambda();
$staticLambda();
推荐答案
是的,这是 5.4+ 中完全有效的语法.
Yes, that is perfectly valid syntax in 5.4+.
基本上,它可以防止当前类自动绑定到闭包(实际上,它可以防止所有绑定,但稍后会详细介绍).
Basically, it prevents auto-binding of the current class to the closure (in fact, it prevents all binding, but more on that later).
class Foo {
public function bar() {
return static function() { var_dump($this); };
}
public function baz() {
return function() { var_dump($this); };
}
}
如果我们在 5.4+ 上实例化它,闭包 bar()
返回的 $this
将设置为 null.就像您对它进行了静态调用一样.但是 baz()
会将 $this
设置为您调用 baz()
的 foo 实例.
If we instantiate that on 5.4+, the closure bar()
returns will have $this
set to null. Just as if you made a static call to it. But baz()
would have $this
set to the foo instance you called baz()
on.
所以:
$bar = $f->bar();
$bar();
结果:
注意:未定义变量:this in/in/Bpd3d on line 5
Notice: Undefined variable: this in /in/Bpd3d on line 5
空
和
$baz = $f->baz();
$baz();
结果
object(Foo)#1 (0) {
object(Foo)#1 (0) {
}
有意义吗?太好了.
现在,如果我们采用在函数外部定义的闭包会发生什么:
Now, what happens if we take closures defined outside of a function:
$a = function() { var_dump($this); };
$a();
我们得到 null
(和一个通知)
We get null
(and a notice)
$c = $a->bindTo(new StdClass());
$c();
我们得到 StdClass
,正如你所期望的
We get StdClass
, just as you'd expect
$b = static function() { var_dump($this); };
$b();
我们得到 null
(和一个通知)
We get null
(and a notice)
$d = $b->bindTo(new StdClass());
$d();
这就是事情变得有趣的地方.现在,我们得到一个警告、一个通知和 null:
This is where things get interesting. Now, we get a warning, a notice, and null:
警告:无法在第 12 行的/in/h63iF 中将实例绑定到静态闭包
Warning: Cannot bind an instance to a static closure in /in/h63iF on line 12
注意:未定义变量:this in/in/h63iF on line 9
Notice: Undefined variable: this in /in/h63iF on line 9
空
因此在 5.4+ 中,您可以声明一个静态闭包,这将导致它永远不会将 $this
绑定到它,也永远不能将一个对象绑定到它...
So in 5.4+, you can declare a static closure, which results in it never getting $this
bound to it, nor can you ever bind an object to it...
这篇关于PHP 静态匿名函数真的有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!