Anonymous functions and closures (PHP 5.3)

Discussion in 'PHP' started by premiumscripts, Aug 18, 2009.

  1. #1
    For those interested in PHP 5.3, here's an interesting article (part of a series) on anonymous functions:

    http://www.recessframework.org/page/functional-php-anonymous-functions-lambdas-closures

       <?php  
       $lambda = function() {   
                   echo "I am an anonymous function,   
                         aka a lambda!<br />";  
                   };  
       $anonymousFunction = $lambda;  
       $anonymousFunction();   
       // Output: I am an anonymous function, aka a lambda!  
         
       function nCallsTo($n, $function) {  
           for($i = 0; $i < $n; $i++) {  
               $function();  
           }  
           return function() { echo "I am also an anonymous function!<br />"; };  
       }  
         
       $anotherAnon = nCallsTo(3, $anonymousFunction);  
       // Output:  
       // I am an anonymous function, aka a lambda!  
       // I am an anonymous function, aka a lambda!  
       // I am an anonymous function, aka a lambda!  
         
       $anotherAnon();  
       // Output: I am also an anonymous function!  
       ?>  
    PHP:
     
    premiumscripts, Aug 18, 2009 IP
    crivion likes this.
  2. tomazinis

    tomazinis Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Did you use this feature in your job?
     
    tomazinis, Aug 19, 2009 IP
  3. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #3
    lol, very interesting but honestly I dont see the utility of anonymous functions
     
    crivion, Aug 19, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'm not using the functionality of PHP 5.3 at the moment because I need compatability for older versions of PHP, however, it's always good to keep up to date on the latest happenings.
     
    premiumscripts, Aug 19, 2009 IP