PHP hook System

Source: Internet
Author: User
Implementation is similar to the hook class in wordpress: classHook {??? Actionhooksarray ????? Privatestatic $ actionsarray ();??? **???? * Adsafunctiontoanactionhook ???? * @ Param $ hook ???? * @ Param $ function ???? *??? Publicstaticfunctionadd_acti

Implement a class similar to the Hook in 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

Implementation is similar to the hook class in 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);??? }

Example:

// Add Hook: add_action ('unique _ name_hook ', 'some _ class: hook_test'); // or use a shortcut function to add a Hook: add_action ('unique _ name_hook ', 'other _ class: hello'); add_action ('unique _ name_hook', 'some _ public_function '); // execute the Hook do_action ('unique _ name_hook '); // You can also use the Hook: do_action ();

Original article: http://neo22s.com/php-hook-system/

Original article address: PHP hook system, thanks to the original author for sharing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.