我知道之间的差异Lambda
and Closures
。我不想用Closure
因为它得到了它的环境,并且var_dump()
- 它将产生大量的输出。使用 lambda 与create_function()
看起来是个好主意,但它正在被弃用。那么用什么来创建在其环境中不知道的函数呢?
Use 静态闭包 http://php.net/manual/en/functions.anonymous.php#functions.anonymous-functions.static:
从 PHP 5.4 开始,匿名函数可以静态声明。这
防止他们将当前类自动绑定到
他们。对象在运行时也可能不绑定到它们。
<?php
class Foo
{
function __construct()
{
$func = static function() {
var_dump($this);
};
$func();
}
};
new Foo();
?>
yields
Notice: Undefined variable: this in %s on line %d
NULL
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)