搜索
系统检测到您的用户名不符合规范:

PHP钩子系统

浏览:15834 发布日期:2013年09月19日 分类:系统代码 关键字: 钩子
实现类似于wordpress中的钩子的类:
class Hook
{
//action hooks array
private static $actions = array();
/**
* ads a function to an action hook
* @param $hook
* @param $function
*/
public static function add_action($hook,$function)
{
$hook=mb_strtolower($hook,CHARSET);
// create an array of function handlers if it doesn't already exist
if(!self::exists_action($hook))
{
self::$actions[$hook] = array();
}
// append the current function to the list of function handlers
if (is_callable($function))
{
self::$actions[$hook][] = $function;
return TRUE;
}
return FALSE ;
}
/**
* executes the functions for the given hook
* @param string $hook
* @param array $params
* @return boolean true if a hook was setted
*/
public static function do_action($hook,$params=NULL)
{
$hook=mb_strtolower($hook,CHARSET);
if(isset(self::$actions[$hook]))
{
// call each function handler associated with this hook
foreach(self::$actions[$hook] as $function)
{
if (is_array($params))
{
call_user_func_array($function,$params);
}
else
{
call_user_func($function);
}
//cant return anything since we are in a loop! dude!
}
return TRUE;
}
return FALSE;
}
/**
* gets the functions for the given hook
* @param string $hook
* @return mixed
*/
public static function get_action($hook)
{
$hook=mb_strtolower($hook,CHARSET);
return (isset(self::$actions[$hook]))? self::$actions[$hook]:FALSE;
}
/**
* check exists the functions for the given hook
* @param string $hook
* @return boolean
*/
public static function exists_action($hook)
{
$hook=mb_strtolower($hook,CHARSET);
return (isset(self::$actions[$hook]))? TRUE:FALSE;
}
}

/**
* Hooks Shortcuts not in class
*/
function add_action($hook,$function)
{
return Hook::add_action($hook,$function);
}

function do_action($hook)
{
return Hook::do_action($hook);
}
用法举例://添加钩子
Hook::add_action('unique_name_hook','some_class::hook_test');
//或使用快捷函数添加钩子:
add_action('unique_name_hook','other_class::hello');
add_action('unique_name_hook','some_public_function');
//执行钩子
do_action('unique_name_hook');//也可以使用 Hook::do_action();
收藏
niceeo
积分:5698 等级:LV4
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /