在PHP中关闭或create_function [英] Closures or create_function in PHP

查看:210
本文介绍了在PHP中关闭或create_function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经决定对我的回调使用闭包,而不是 create_function ,因此只支持PHP> 5.3主要是由于增加了可调试性,也因为我假设它是什么关于假设?),在我的情况下的 create_function 的即时编译的开销可能会抵消任何额外的比较,



这可能仍是这种情况(对于我的应用程序),需要进一步测试,但我对这个输出感兴趣非常)简单的测试,这表明 create_function 方法是闭包的两倍快,当它可以只删除四个条件(和串联)。显然,在我的测试用例中没有额外的处理,这是大多数的速度将获得或丢失,但在你没有额外的处理,但很多条件(可以删除)和回调被调用了足够的次数我开始认为可能最好使用 create_function



create_function eval 之间的相似性,我很担心。



因此,主要问题是使用 create_function 创建的匿名函数和闭包的匿名函数之间有什么区别?



我想到的几个具体问题是 create_function eval 功能被禁用?并且,我相信我最近读一个地方, create_function 函数将污染全局(或类)命名空间,即使声明为内部函数,但闭包不会。我现在找不到对此的引用,但是这些语句是真的吗?






这是小测试我跑:

 <?php 

function foo($ a = true,$ b = true,$ c = true,$ d = true)
{
$ inner1 = create_function(
'',
'$ r = \''。 a?'a':'')。
($ b?'b':'')。
($ c?'c':'')。 'd':'')。'\';
return $ r;'
);


$ inner2 = function()use($ a,$ b,$ c,$ d)
{
$ r =''
if($ a){$ r。='a'; }
if($ b){$ r。='b'; }
if($ c){$ r。='c'; }
if($ d){$ r。='d'; };
return $ r;
};


$ time = microtime(true);
for($ x = 0; $ x< 99999; ++ $ x)
{
$ i1out = $ inner1();
}
echo'1:'(microtime(true) - $ time)。'< br>';

$ time = microtime(true);
for($ x = 0; $ x< 99999; ++ $ x)
{
$ i2out = $ inner2();
}
echo'2:'。(microtime(true) - $ time)。'< br>';

echo var_dump($ i1out === $ i2out)。'< br>';
}

foo();
{..}
是一个匿名函数,此功能通常与关闭 a>。 create_function 也不会匿名函数污染全局命名空间。



由于匿名函数可以访问周围的变量,它们在理论上可以稍慢一点。另一方面,如果你使用字节码缓存(如果你不是,你显然不关心性能),我希望匿名函数的编译开销稍微更慢。



但是,匿名函数和 create_function 之间的区别不太可能是性能问题的根源。因此,如果你很幸运有一个目标平台php> 5.3,我会选择一个更加可读的匿名函数形式。


I had made a decision to use closures for my callbacks instead of create_function and as such only support PHP > 5.3 mostly due to the increased debugability and also because I assumed (what is it they say about assumption?) that the overhead of the on-the-fly compilation of the create_function in my situation would probably offset any extra comparisons and such that had to be made within in the function.

This may well still be the case (for my application) and further testing is required, but I was interested in the output of this (very) simple test, that shows the create_function method being more than twice as fast as the closure when it can remove just four conditionals (and concats). Obviously there is no extra processing going on in my test case, and that is where most of the speed will be gained or lost, but in the case where you have little extra processing but a lot of conditionals (that could be removed) and the callback is called enough times I started to think that it may be better to use create_function.

However with the obvious similarity between create_function and eval, I'm wary of it.

So the main question is what are the differences between anonymous functions created with create_function and those of closures?

A few specific questions I'm thinking of are, will create_function even work when eval functionality is disabled? And, I'm sure I read somewhere recently that create_function functions will pollute the global (or class) namespace even if declared as inner functions, but closures won't. I can't find the reference to this now, but are either or both of those statements true?


This is the little test I ran:

<?php

function foo($a=true, $b=true, $c=true, $d=true)
{
    $inner1 = create_function(
        '', 
        '$r = \''.($a ? 'a' : '').
                  ($b ? 'b' : '').
                  ($c ? 'c' : '').
                  ($d ? 'd' : '').'\';
         return $r;'
    );  


    $inner2 = function() use ($a, $b, $c, $d) 
    {   
        $r = ''; 
        if($a) { $r .= 'a'; }
        if($b) { $r .= 'b'; }
        if($c) { $r .= 'c'; }
        if($d) { $r .= 'd'; };
        return $r; 
    };  


    $time = microtime(true);
    for ($x=0; $x<99999; ++$x)
    {   
        $i1out = $inner1();
    }   
    echo '1:'.(microtime(true)-$time).'<br>';

    $time = microtime(true);
    for ($x=0; $x<99999; ++$x)
    {   
        $i2out = $inner2();
    }   
    echo '2:'.(microtime(true)-$time).'<br>';

    echo var_dump($i1out===$i2out).'<br>';
}

foo();

解决方案

The construct function() {..} is an anonymous function, and this feature is often implemented together with closures. Neither create_function nor anonymous functions pollute the global namespace.

Since anonymous functions can access surrounding variables (the closure part), they can, in theory, be slightly slower. On the other hand, if you're using bytecode caching (and if you're not, you are obviously not concerned about performance), I'd expect the "compilation" overhead of anonymous functions to be slightly slower.

However, it is extremely unlikely that the difference between anonymous functions and create_function is a source of performance problems. Therefore, I'd choose the more readable form of an anonymous function if you are so fortunate to have a target platform with php>5.3.

这篇关于在PHP中关闭或create_function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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