note 98709 deleted from function.call-user-func by danbrown
| From: | danbrown@php.net | Date: | Thu, 01 Jul 2010 23:45:39 +0000 |
| Subject: | note 98709 deleted from function.call-user-func by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170646@lists.php.net to get a copy of this message | ||
Note Submitter: f4bi0_ at hotmail dot com
----
In addition to edgar dot klerks at gmail dot com
Sometimes we may want to append the same mechanical functionality
to the end of one or more then one class methods at run-time period
consider this very brief class sample:
<?php
class User
{
private $_country;
private $_name;
private $_id;
public function setCountry($country){
$this->_country=$country;
}
public function setName($name){
$this->_name=$name;
}
public function setID($id){
$this->_id=$id;
}
public function print_r(){
# don't mind this now,
# this is just for displaying results latter
print_r(
array(
'id'=>$this->_id,
'name'=>$this->_name,
'country'=>$this->_country));
}
}
?>
What if you want methods "setCountry","setName" and "setID"
to do the same thing after being called,
say like return the object itself,
thus allowing the so-called 'chaining' stuff?
Normally, you would hardcode the sample "return $this;"
over to the end of all these three methods.
But keep in mind that this routine is something purely optional,
therefore hardcoding it directly could make your script look a little unprofessional.
Besides, what if you have a whole library of classes
and want the same thing to be applied to many different methods at once, dinamically?
Here is the solution:
<?php
class LaterCallback
{
private $methods = array();
private $object;
public function __construct($object)
{
$this->object = $object;
}
public function registerCallbackAfter($callback,array $methods)
{
foreach($methods as $each)
{
$this->methods[$each]=$callback;
}
}
private function callback($method)
{
if( array_key_exists($method,$this->methods) )
{
$callback = $this->methods[$method];
return call_user_func($callback,$this);
}
}
public function __call($method,$args)
{
if(call_user_method_array($method,$this->object,$args));
{
return $this->callback($method);
}
}
public function __get($var){
return $this->object->$var;
}
public function __toString(){
return $this->object->__toString();
}
}
// HOW TO USE IT
// Creates the User object
// wrapped withing the LatterCallback framework
$user = new LaterCallback(new User);
// Register a dirty and anonymous callback
// executed right after setCountry,setName and setID methods are called
$user->registerCallbackAfter(
function($obj){return $obj;},
array('setCountry','setName','setID')
);
// 'chaining' is possible now :)
$user
->setName('Fabio Souto')
->setID(5)
->setCountry('Brazil');
$user->print_r();
// Results: Array ( [id] => 5 [name] => Fabio Souto [country] => Brazil )
?>
Indeed, you could tell the callback to do anything you desire
after the execution of a given array of methods,
say for exemple, update a hidden counter somewhere,
keep track of object calls, etc. ;
You should email me: f4bi0_@hotmail.com