note 110962 added to functions.anonymous
| From: | marcellorvalle at gmail dot com | Date: | Sat, 29 Dec 2012 22:05:32 +0000 |
| Subject: | note 110962 added to functions.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192707@lists.php.net to get a copy of this message | ||
I fell uncomfortable with php's way to pass callbacks ahead. It's just a personal opinion
but i think the structure array($object, 'method') a little bit ugly. In some special
cases i would like to do something more verbose, like that:
<?php
$sortedArray = ArrayServices::sort($arrayOfPerson, $sortPerson->byName);
?>
Here, "$sortPerson->byName" is a pointer to a function that receives 2 instances of
Person and return true if the name of the first is "bigger" than the second.
"ArrayServices::sort" is a function that can sort any kind of array using different
criterias, it uses the function passed on the second parameter to compare two items on the array
I am able do that defining a simple abstract class which gives the ability to it's children to
expose their methods as closures using __get() magic funtion.
<?php
abstract class ClosureExposerObject
{
public function __get($methodName)
{
if (is_callable(
array($this, $methodName)))
{
return function() use ($methodName)
{
$args = func_get_args();
return call_user_func_array(array($this, $methodName), $args);
};
}
else
{
$className = get_class($this);
throw new \BadMethodCallException("$method is not a callable at $className
class.");
}
}
}
?>
Now it is possible to define a class SortPerson so that i could use it like in the first code
snippet.
<?php
class SortPerson extends ClosureExposerObject
{
public function byName(Person p1, Person p2)
{
return p1->getName() > p2->getName();
}
public function byAge(Person p1, Person p2)
{
return p1->getAge() > p2->getAge();
}
public function bySurName(Person p1, Person p2)
(...)
}
?>
I just needed to extend ClosureExposerObject and do nothing more.
Some observations: SortPerson is a service class but i decided not to implement it with static
methods. It is a personal matter but i really dont like to implement such small classes with
statics. Also i have inverted some naming conventions (class names as substantives and methods as
verbs) but in some very special cases i thing that it actually helps readability.
----
Server IP: 69.147.83.199
Probable Submitter: 186.235.110.83
----
Manual Page -- http://php.net/manual/en/functions.anonymous.php
Edit -- https://master.php.net/note/edit/110962
Del: integrated -- https://master.php.net/note/delete/110962/integrated
Del: useless -- https://master.php.net/note/delete/110962/useless
Del: bad code -- https://master.php.net/note/delete/110962/bad+code
Del: spam -- https://master.php.net/note/delete/110962/spam
Del: non-english -- https://master.php.net/note/delete/110962/non-english
Del: in docs -- https://master.php.net/note/delete/110962/in+docs
Del: other reasons-- https://master.php.net/note/delete/110962
Reject -- https://master.php.net/note/reject/110962
Search -- https://master.php.net/manage/user-notes.php