Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6080623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:04:58+00:00 2026-05-23T11:04:58+00:00

I had made a decision to use closures for my callbacks instead of create_function

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:04:58+00:00Added an answer on May 23, 2026 at 11:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had made some questions regarding PHP-GTK (there are only 4 php-gtk tagged questions
Had a page that was working fine. Only change I made was to add
I had made a main page and use user component to implement the style
I had made changes in a folder called branch, and now that its working
I had made a windows service in visual studio 2008 in C#. inside the
EDIT: I had made a mistake during the debugging session that lead me to
I Made label to had value from database and linked it . but the
I have a smallish utility library I made that I had created in TFS
I once had a MySQL database table containing 25 million records, which made even
Some of the answers to a question I had about redirect_to made me think

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.