Re: PATCH: anonymous functions in PHP

From: Date: Thu, 22 Mar 2007 15:12:47 +0000
Subject: Re: PATCH: anonymous functions in PHP
References: 1 2 3 4 5 6 7 8 9 10 11  Groups: php.internals 
Request: Send a blank email to internals+get-28553@lists.php.net to get a copy of this message
Michael Walter wrote: > A simple real-life example is: > function pick($member) > { > return create_function('$x', 'return $x->'.$member.';'); > } > > which could be used e.g. in > array_map(pick('age'), $people). > Arguably, in this case you could as well write: > array_map(function($p) {return $p->age;}, $people); > but I believe it is easy to see how the problem generalizes. Funnily enough you already show how this can currently be done. My point would be to make simple things simple and keep complicated things possible. As it is right now. So I'd use array_map(function($p) {return $p->age;}, $people); or usort($a, function($a, $b) { return strcmp($a->lastname, $b->lastname); }); to do Stefan Walk's example. But something like your pick function using create_function if I *really* wanted something more fancy. - Chris

« previous php.internals (#28553) next »