Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

In this case it isn't so weird, since PHP pre 5.3 didn't have first class functions. To pass a function around, you would use a variable containing a string of it's name.

create_function was a way to

A) not having to define a function separately

B) not getting problems with the function being re-defined, since each call would create a new function

C) fake closures by generating code.

All this should be moot points by now, since PHP has real anonymous functions with closures,



That's true (except for C), because I can't see how a create_function-defined function closes over its environment), but what I had in mind was the way in which the author of the documentation, obviously one of the PHP core developers, talks casually about "returning the name of an anonymous function". It shows just how much twisted the logic of these people is.

But I suppose that goes naturally hand in hand with the cargo cult approach to language design.


> That's true (except for C), because I can't see how a create_function-defined function closes over its environment),

That's why it would be a fake closure.

    $foo = someNumber();
    create_function('', 'return '.$foo.';');
You would generate a new string to be evaluated as the function body each time.

> talks casually about "returning the name of an anonymous function". It shows just how much twisted the logic of these people is.

I think you read too much into this. The point of an anonymous function isn't to make it not have a name, but to be able to define it where it is needed instead of referring to some specific function name in your code.

It also fits well with the way PHP handles "pointers", by storing the name of a variable in another variable.

    $foo = 42;
    $bar = 'foo';
    print($$bar); // Prints 42.


"The point of an anonymous function isn't to make it not have a name, but to be able to define it where it is needed instead of referring to some specific function name in your code."

Actually, its point is to make it a value that can be referenced from any number of bindings (associations between names and values) in any number of scopes. What you're saying is just a consequence of this.

"It also fits well with the way PHP handles "pointers", by storing the name of a variable in another variable."

Which only shows the deficiency, since any such indirect reference should never refer to a name, but to a binding instead.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: