Explanation of source code of CI framework-method for using Hook. php file for Function Extension-php instance

Source: Internet
Author: User
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.

Related Article

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.