[Pmwiki-users] Hook object
Christian Ridderström
chr
Tue Feb 17 09:46:00 CST 2004
Hi
I decided to implement hooks using a class object instead, using a
class I call 'Hook'. In the beginning of my config.php I now do this:
$customHook = new Hook();
$customHook->add('addCurrentGroupToSearchBox');
and then after including stdconfig.php I run the hook like this:
$customHook->run();
/Christian
PS. Here's the implementation.
/** Class: Hook
Implements a 'hook', i.e. the user can 'append' or 'prepend' names of
functions to a list, and then a at a later time 'run the hook'.
!!Usage
Create a hook object like this:
$customHook = new Hook(); // Create a Hook object
Then append a function called 'helloWorld' like this:
$customHook->append('helloWorld');
as an alias for @@append()@@, you can also use @@add()@@. To prepend a
function to the hook use @@prepend()@@ instead. Finally, the hook is
run like this:
$customHook->run();
!!Tip
The function @@create_function()@@ is useful to create temporary
functions, e.g. like this:
$customHook->add(create_function('', 'echo "Hello World\n";'));
*/
class Hook {
var $hooks_;
function Hook() { $this->hooks_ = array(); }
function hooks(){ return $this->$hooks_; }
function append($fcn){ array_push($this->hooks_, $fcn); }
function prepend($fcn){ array_splice($this->hooks_,0,0,array($fcn)); }
function add($fcn){ $this->append($fcn); }
function run(){
array_map('call_user_func',
array_filter($this->hooks_, 'function_exists'));
}
function show(){
echo "<pre>hooks_=\n";
foreach($this->hooks_ as $k=>$v)
printf("%s => '%s'\n", $k, $v[0] == "\0" ? "\\0".substr($v, 1) : $v);
echo "</pre>";
}
}
--
Christian Ridderstr?m http://www.md.kth.se/~chr
More information about the pmwiki-users
mailing list