PHP's create_function() versus just using eval()
Asked Answered
M

5

4

In PHP you have the create_function() function which creates a unique named lambda function like this:

$myFunction = create_function('$foo', 'return $foo;');
$myFunction('bar'); //Returns bar

Is this actually any better (apart from being more easy) then just doing:

do{
 $myFunction = 'createdFunction_'.rand();
}
while(function_exists($myFunction));
eval("function $myFunction(\$foo) { return \$foo; }");
$myFunction('bar'); //Returns bar

Is create_function really better? (apart from the fact that it is more easy)

Marsiella answered 6/1, 2009 at 23:47 Comment(0)
S
5

On my understanding of the relevant docs,[1] they both do the same thing, create_function() just comes up with a unique function name for you.

To address some other comments on this question:

create_function can be assigned to a variable making the function accessible to other parts of your code, whereas eval is only useful for the given scope.

It may well be that eval() runs in the current scope, but function definitions get dumped into the global namespace anyway.[2] So whenever you define a function, it will be accessible everywhere else in your program.

Using eval() will clutter the global function list, create_function() will not

create_function() only returns a string with the name of the new function,[3] not some special callback type. So, both techniques will pollute your global namespace.

So no, apart from create_function() being easier, it does not appear to be any better than eval().

Footnotes:

[1] https://www.php.net/manual/en/functions.user-defined.php ; http://au.php.net/create_function ; http://au.php.net/eval

[2] https://www.php.net/manual/en/functions.user-defined.php

[3] http://au.php.net/create_function

Star answered 13/3, 2009 at 11:53 Comment(0)
D
6

Using eval() will clutter the global function list, create_function() will not, apart from that there's no big difference. However, both methods require writing the function body inside a PHP string which is error-prone and if you were working on my project I would order you to just declare a helper function using the normal syntax.

Anonymous functions in PHP are so poorly implemented that your code is actually better off not using them. (Thankfully this will be fixed in PHP 5.3).

Dispensatory answered 7/1, 2009 at 0:29 Comment(2)
I love the JS function() { syntax, which is how i came across this, trying to find out of something similar is possible in PHP.Marsiella
It sucks compared to JS's implementation, and from what I remember pretty slow.Ashleighashlen
S
5

On my understanding of the relevant docs,[1] they both do the same thing, create_function() just comes up with a unique function name for you.

To address some other comments on this question:

create_function can be assigned to a variable making the function accessible to other parts of your code, whereas eval is only useful for the given scope.

It may well be that eval() runs in the current scope, but function definitions get dumped into the global namespace anyway.[2] So whenever you define a function, it will be accessible everywhere else in your program.

Using eval() will clutter the global function list, create_function() will not

create_function() only returns a string with the name of the new function,[3] not some special callback type. So, both techniques will pollute your global namespace.

So no, apart from create_function() being easier, it does not appear to be any better than eval().

Footnotes:

[1] https://www.php.net/manual/en/functions.user-defined.php ; http://au.php.net/create_function ; http://au.php.net/eval

[2] https://www.php.net/manual/en/functions.user-defined.php

[3] http://au.php.net/create_function

Star answered 13/3, 2009 at 11:53 Comment(0)
I
3

Personally, I've found that create_function() is extremely handy when sorting arrays.

In fact, I just searched the web, and it seems that the PHP documentation has a good example of this.

http://us.php.net/create_function

Scroll down to Example #3 Using anonymous functions as callback functions.

Intelligence answered 6/1, 2009 at 23:51 Comment(0)
D
2

create_function can be assigned to a variable making the function accessible to other parts of your code, whereas eval is only useful for the given scope.

Deckle answered 7/1, 2009 at 0:6 Comment(1)
This is not correct. Try creating a function with eval within a function. You will be able to call it outside the functino, too. function a() { eval('function b(){echo "hi\n";}'); } a(); b();Hammers
T
0

(apart from the fact that it is more easy)

I don't understand how you can dismiss this so readily. Given your two examples, which is easier to understand at a glance? Create_function tells you what you intend to accomplish. Eval doesn't.

Treasonable answered 7/1, 2009 at 1:27 Comment(1)
Mostly because I wanted to know if there were any other PRO's.Marsiella

© 2022 - 2024 — McMap. All rights reserved.