Php, what to use instead of create_function()?

2019-07-21 04:19发布

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?

标签: php lambda
1条回答
男人必须洒脱
2楼-- · 2019-07-21 04:45

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
查看更多
登录 后发表回答