<?php
/*定义钩子函数*/
function add($hook,$actionFunc)
{
global $emHooks;
if(isset($emHooks[$hook]))
{
if(!in_array($actionFunc, $emHooks[$hook]))
{
$emHooks[$hook][] = $actionFunc;
}
}
else
{
$emHooks[$hook][] = $actionFunc;
}
return true;
}
/*执行挂载钩子上的函数*/
function doo($hook)
{
global $emHooks;
$args = array_slice(func_get_args(),1);
if(isset($emHooks[$hook]))
{
foreach ($emHooks[$hook] as $fun)
{
# code...
$string = call_user_func_array($fun, $args);
}
}
else
{
echo "not hook:".$hook;
}
}
/*业务函数*/
function cnt()
{
echo "dddd<br>";
}
/*业务函数*/
function say()
{
echo "this is a hook";
}
//注册钩子
add("index","cnt");
add("index","say");
//在改调用的地方 调用钩子
doo("index");