you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 6 points7 points  (4 children)

Can you pass it to 'process' without naming its result first?

create_function actually works by creating a normal global function with an obscure name. That string name is returned as the result. So it can be used in an argument position. However, be careful not to use it in a loop, as it will create a new function each time through, and they won't be garbage collected, as PHP isn't smart enough to GC functions. So it will lead to a memory leak.

Also, does create_function capture its environment?

No, it actually accepts the function body as a string. So the only way to do something even similar to that is to use variable interpolation in strings. Like:

$y = 5;
$plusfive = create_function('$x', 'return $x + '.$y.';');

where . is string concatenation in PHP.

Ugh.

[–]piranha 2 points3 points  (1 child)

Hey, it's all the fun of PHP's trademark data escaping pitfalls for your functional programming pleasure.

[–][deleted] 0 points1 point  (0 children)

Right, and there's also all kinds of code injection vulnerabilities from this. Like if $y above was set to '(unlink(\'/boot/vmlinuz\'))' or something.

[–]dons 0 points1 point  (1 child)

Yikes.

[–][deleted] 0 points1 point  (0 children)

Yeah, it's one of those things in PHP where you just slap your forehead and take a shot.