Rails and Stuff at Abel killed Cain

Ruby on Rails and other Webdevelopment

Archive for March, 2010

PHP 5.3 lambda use case

March 6, 2010 General Comments

i just thought about iterating objects in php.

Since 5.3 supports lambda, i found this quite usefull:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Test {
        public $vals = array();

        function __construct(){
                $this->vals = range(1,10);
        }

        // the hooray goes here!
        function each($function){
                foreach($this->vals as $element){
                        $function($element);
                }
        }
}

$t = new Test;

//the real hooray-part:
$t->each( function( $e ) {
        echo "Hi, i am element $e\n";
});

Works! :)
[More]