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

To be honest, I'm not quite sure what this article is trying to point out; lambda functions (and functional-style programming) have been a part of the PHP language for a very long time -- since PHP4 to be exact. It may not be as "sexy" to look at as Lisp et al, but it's certainly been doable.

    $fn = create_function( "$a", "return $a * 2" );
    $list = array( 1, 2, 3, 4 );
    $double = array_map( $fn, $list ); // array( 2, 4, 6, 8 )
or

    function my_filter( $a ) { return $a > 10 ? true : false; }
    $list = array( 5, 10, 15, 20 );
    $less = array_filter( $list, "my_filter" ); // array( 15, 20 )


there's a real problem associated with using create_function:

It's not much more than a glorified eval(). Evaluation takes place at runtime, so you don't notice syntax errors until it's too late. Bytecode-Caches can't cache your function and as the function body is a string, you won't get syntax highlighting in most editors.

Also, create_function really creates a named (with a random name in the form lambda_number) function in the global scope. Functions in general are all globally scoped and come to life once the execution reaches the function declaration. So this actually works:

    function a(){
      function b(){ echo "gnegg\n"; }
    }
    a();
    b();
Whereas

    function a(){
      function b(){ echo "gnegg\n"; }
    }
    b();
wouldn't.

This also implies that no scope is captured and you can't access any variables of the outside body of code.

5.3 brings real anonymous functions and provides a way to conserve the enclosing scope (thus allowing you to create closures), though the syntax is a bit awkward and variables of the outer scope you want to access need to be declared one by one.


Yes, every single point you made is correct, valid, and of much concern. My point was simply that everything the article covers was already completely doable since PHP4, even if it wasn't the most optimal solution. It was more a teardown of the article than the features of PHP 5.3, which I would be taking advantage of in a heartbeat if only people would actually upgrade their PHP installs in a timely manner...




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

Search: