After reading the source code of hook. php, we can see how CI uses hook for extension.
Hook basic knowledge http://codeigniter.org.cn/user_guide/general/hooks.html
The use of the hook in CI goes through the process of enabling the hook, defining the hook, calling the hook, and executing the hook.
The manual provides instructions for enabling, defining, and calling methods. So what is the implementation principle of hook.
Welcome to exchange: mailto: actionbi2010@gmail.com more source read
<? PHP if (! Defined ('basepath') Exit ('no direct Script Access allowed'); Class ci_hooks {var $ enabled = false; // The Hook flag is disabled by default. The configuration in apppath/config. php is disabled by default. If you want to use hook, enable it in config. php. VaR $ hooks = array (); // apppath/config/hook during _ initialize () function initialization. the hook array defined in PHP is referenced to $ this-> hooks; var $ in_progress = false; // when a hook is executed, $ in_process = true is marked, to prevent the same hook from being called at the same time. Function _ construct () {$ this-> _ initialize (); log_message ('debug', "hooks class initialized");} function _ initialize () {// initialize the hook // judge config. whether to enable hook // include (Hook. PHP), reference the hook array defined in the file to $ this-> hooks // $ this-> enable = true} function _ call_hook ($ which = '') // pre_system {// take the pre_system hook point as an example. When _ call_hook ('pre _ system') is called ') when // make sure $ this-> enable = true & defines $ this-> hooks ['pre _ system'] // if it is a two-dimensional array, traverse, and _ Run_hook ($ this-> hooks ['pre _ system'] [$ Val]) // if it is a one-dimensional array, then _ run_hook ($ this-> hooks ['pre _ system'])} function _ run_hook ($ data) // $ data is the passed hook Array {// $ data is stored in apppath/config/hook. hook array defined by PHP // $ hook ['pre _ controller'] = array (// 'class' => 'myclass ', // 'function' => 'myfunction', // 'filename' => 'myclass. PHP ', // 'filepath' => 'hooks', // 'params' => array ('beer', 'wine', 'snacks ')//); retrieve dat Load apppath. $ data ['filepath']. $ data ['filename']. instantiate the hook class and call the function. In the application example, $ this-> in_process = true; $ hook = new myclass (); $ hook-> myfunction ($ Params ); $ this-> in_process = false ;}}?>
The hook points can be attached to multiple hooks. Therefore, when we want to expand the CI, we only need to put the hook file under the apppath folder, and then
You can declare the defined hook information in apppath/config/hook. php. When the system runs to the hook point, the declared hook is automatically called.
In this way, scalability is achieved.