php,用什么代替create_function()? [英] Php, what to use instead of create_function()?

查看:332
本文介绍了php,用什么代替create_function()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道LambdaClosures之间的区别.我不想使用Closure,因为它得到了它的环境,并且var_dump()-它将产生大量的输出.将lambda与create_function()一起使用似乎是个好主意,但已被弃用.那么该怎么用来创建对环境不了解的功能呢?

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 5.4开始,匿名函数可以静态声明.这 阻止他们将当前类自动绑定到 他们.对象也可能在运行时未绑定到它们.

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();

?>

收益

Notice: Undefined variable: this in %s on line %d
NULL

这篇关于php,用什么代替create_function()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆