Codeigniter source code reading (6) implementation principle of hook. php extension framework

Source: Internet
Author: User

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.

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.