> php教程 > php手册 > 본문

PHP钩子系统

WBOY
풀어 주다: 2016-06-06 20:07:24
원래의
822명이 탐색했습니다.

实现类似于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_acti

实现类似于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();
로그인 후 복사

原文:http://neo22s.com/php-hook-system/

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!