title: Closures or create_function in PHP
body: <p>I had made a decision to use closures for my callbacks instead of <code>create_function</code> 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 <code>create_function</code> in my situation would probably offset any extra comparisons and such that had to be made within in the function.</p>
  
  <p>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 <code>create_function</code> 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 <code>create_function</code>.</p>
  
  <p>However with the obvious similarity between <code>create_function</code> and <code>eval</code>, I'm wary of it. </p>
  
  <p>So the main question is what are the differences between anonymous functions created with <code>create_function</code> and those of closures? </p>
  
  <p>A few specific questions I'm thinking of are, will <code>create_function</code> even work when <code>eval</code> functionality is disabled? And, I'm sure I read somewhere recently that <code>create_function</code> 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?</p>
  
  <hr>
  
  <p>This is the little test I ran:</p>
  
  <pre><code>&lt;?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&lt;99999; ++$x)
      {   
          $i1out = $inner1();
      }   
      echo '1:'.(microtime(true)-$time).'&lt;br&gt;';
  
      $time = microtime(true);
      for ($x=0; $x&lt;99999; ++$x)
      {   
          $i2out = $inner2();
      }   
      echo '2:'.(microtime(true)-$time).'&lt;br&gt;';
  
      echo var_dump($i1out===$i2out).'&lt;br&gt;';
  }
  
  foo();
  </code></pre>