I know the differences between Lambda
and Closures
. I dont want to use Closure
since it gets its environment, and var_dump()
-ing it would result a tons of output. Using lambda with create_function()
looked a good idea, but its getting deprecated. Then what to use to create functions that not aware in their enviromnent?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Use static closure:
As of PHP 5.4, anonymous functions may be declared statically. This prevents them from having the current class automatically bound to them. Objects may also not be bound to them at runtime.
<?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