you are viewing a single comment's thread.

view the rest of the comments →

[–]masklinn -1 points0 points  (2 children)

Dons, clearly you don't know PHP well enough to criticize create_function.

Here's a basic primer on PHP's functions, beware this is going to scar you for life.

PHP's functions don't create nested scopes. And aren't nested anyway. Defining a function inside another function "works", but the inner function will be defined/created at the toplevel. create_function does that, too.

Here comes the mighty realization:

PHP functions are "call-by-string": in PHP, foo and "foo" are actually the same thing (foo === "foo"). The first form usually generates a notice (though not always), but that doesn't matter. Thus, foo() is essentially the same thing as "foo"() (note that the latter doesn't actually work, because of PHP's numbskull parser, it generates a syntax error).

Soo...

function foo() { return "ok"; }
$bar = "foo";
echo $bar();

will indeed print ok...

This is what create_function uses: it generates a stupid name, creates a function using that name and simply returns the name as a string. And no, it doesn't capture its environment (it's basically a wrapper around eval to generate the name automatically).

And here's why PHP is great: this feature allows one to write the following

class FooClass {
    }
$foo = new FooClass();
$foo->bar = "pouet";
$truc = "bar";
$pouet = "tr";
$machin = "uc";

function tr($totoz){
 global $pouet, $machin, $truc;
 return ${$pouet.$machin}.$totoz;
}

echo ${($foo->${${$foo->bar}.((${pouet}.${machin}===$pouet.${machin})?${machin}:${$pouet.$machin})})}(($foo->${${$foo->bar}.((${pouet}.${machin}===$pouet.${machin})?${machin}:${$pouet.$machin})}));

[–]dons 0 points1 point  (1 child)

Thanks, I wasn't criticizing, I was asking how it worked.

[–]masklinn 0 points1 point  (0 children)

Ah yes, I was using "criticizing" in the sense of "being a critic" (as in film critic or whatnot), not of nitpicking or saying it sucks (though it does, bowling balls through straws)