This article mainly introduces how to use Hook to interpret the source code of the CI framework. the Function Extension Method of PHP files is analyzed. The Hook principle and the related skills of extending the CI framework are analyzed. If you need a friend, refer to the example in this article to describe how to use the Hook to interpret the source code of the CI framework. PHP file Function Extension Method. We will share this with you for your reference. The details are as follows:
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.
<? 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 d 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 ;}}?>
Multiple hooks can be attached to the hook points. Therefore, when we want to expand the ci, we only need to put the hook file under the APPPATH folder and then to APPPATH/config/hook. the defined hook information can be declared in php. When the system runs to the hook point, the declared hook is automatically called.
In this way, scalability is achieved.