Re: PATCH: anonymous functions in PHP

From: Date: Thu, 22 Mar 2007 14:49:15 +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-28550@lists.php.net to get a copy of this message
> function sort_by_key($key) { For the record, I do like this syntax, but it COULD be rewritten using current practices (see below). class SortByKey { public $key; public function __construct($key) { $this->key = $key; } public function do($a, $b) { if ($a[$this->key] < $b[$this->key]) return -1; if ($a[$this->key] == $b[$this->key]) return 0; return 1; } } $a = array( array('id' => 0, 'first_name' => 'john', 'last_name' => 'connor'), ... ); $Sorter = new SortByKey('last_name'); usort($a, array($Sorter, 'do')); That said, I like the idea of a first class callable object. For the example above, doing this: class SortByKey implements Callable { ... $Sorter(); Would actually execute $Sorter->do(); (obviously there's a problem when $Sorter has a __toString (dynamic function call), but this seems to nicely wrap all but the most basic case of callback, where an object might be overkill. Perhaps this could be implemented in SPL? (Just wondering if it's possible at this point, I'm not campaigning for it..) S

« previous php.internals (#28550) next »