PHP静态匿名函数真的有效吗?
我正在尝试学习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
为什么此代码行不起作用返回静态函数(){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();
结果:
注意:未定义的变量:第5行的/in/Bpd3d中的变量
Notice: Undefined variable: this in /in/Bpd3d on line 5
NULL
还有
$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
(和通知)
$c = $a->bindTo(new StdClass());
$c();
正如您所期望的那样,我们得到StdClass
We get StdClass
, just as you'd expect
$b = static function() { var_dump($this); };
$b();
我们收到null
(和通知)
$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
注意:未定义的变量:第9行的/in/h63iF中的变量
Notice: Undefined variable: this in /in/h63iF on line 9
NULL
因此在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...