Eval function in PHP allows to dynamically create code, which is neat (for people that like such things 😉
$classname = “foo”;
eval(“\$foo = $classname::bar;”);
However, there’s a problem with that – it’s slow. It means each time one has to compile code anew and then execute it anew. Slowwww. There’s another method – create_function() – which allows to do like this:
$newfunc = create_function(‘$a,$b’, ‘return “ln($a) + ln($b) = ” . log($a * $b);’);
echo “New anonymous function: $newfunc\n”;
echo $newfunc(2, M_E) . “\n”;
But the problem would be that it creates a new function – meaning, you can not share symbol table with this code, and you need brand new execution context each time you call it, which is slow too. So, won’t it be nice if you could combine best of the two functions and have code that can be compiled once, but run a number of times while sharing symbol table with the calling code? Would work almost like lambda – though not exactly like – it would not bind variables until really executing the code.
But, if we could use both benefits of create_function() as to making function objects and compiling it only once – and of instant same-scope-ness of eval() – it might allow to do some nice things.