I am creating a action-hook system for a php application.
Here is what I have done so far.
$where is the name of the hook
$priority decides the order followed when there are more than one actions for one hook location.
(hook::execute() is called when a hook location is reached and my application core runs any hooked actions)
class hooks{
private $hookes;
function __construct()
{
$hookes=array();
}
function add_action($where,$callback,$priority=50)
{
if(!isset($this->hookes[$where]))
$this->hookes[$where]=array();
$this->hookes[$where][$callback]=$priority;
}
function remove_action($where,$callback)
{
if(isset($this->hookes[$where][$callback]))
unset($this->hookes[$where][$callback]);
}
static function compare($a,$b)
{
return $a>$b?1:-1;
}
function execute($where)
{
if(isset($this->hookes[$where])&&is_array($this->hookes[$where]))
{
usort($this->hookes[$where],"hook::compare");
foreach($this->hookes[$where] as $callback=>$priority)
{
call_user_func($callback);
}
}
}
};
My question is what to do in execute($where) to have it accept a variable argument list and pass them in call_user_func($callback);
For different calls to execute, there may be variable number of parameters to be passed in the callback.
You can use call_user_func_array function, second argument is array with arguments