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! :)
Ah and yes, for idiots not knowing what happens on execution:
1 2 3 4 5 6 7 8 9 10 11 | $ php test.php Hi, i am element 1 Hi, i am element 2 Hi, i am element 3 Hi, i am element 4 Hi, i am element 5 Hi, i am element 6 Hi, i am element 7 Hi, i am element 8 Hi, i am element 9 Hi, i am element 10 |
Write a Comment