CI Framework source code interpretation of the use of hook.php file to complete the function extension method, _php tutorial

Source: Internet
Author: User
Tags php definition codeigniter zend framework

CI Framework source code interpretation of the use of hook.php file to complete the function extension method,


This paper describes the method of using hook.php file to complete the function extension of CI framework. Share to everyone for your reference, as follows:

Read the source of the hook.php, we know that CI using hooks to expand the principle.

Basic knowledge of Hooks http://codeigniter.org.cn/user_guide/general/hooks.html

The use of hooks in CI has undergone a process of opening hooks, defining hooks, calling hooks, and executing hooks.

The method of opening, defining, and invoking is already informed in the manual. So what is the principle of hook implementation?

<?php if (! defined (' BasePath ')) exit (' No Direct script access allowed '); class Ci_hooks {var $enabled = FALSE; The flag that opens the hook is turned off by default. The configuration in apppath/config/config.php is also turned off by default, and if you want to use hooks, open them in config.php. var $hooks = array (); In the process of initializing the _initialize () function, an array of hooks defined in apppath/config/hook.php is referenced to $this->hooks; var $in _progress = FALSE; When a hook executes, it gives the token $in _process = TRUE to prevent the same hook from being invoked simultaneously.  function __construct () {$this->_initialize (); Log_message (' Debug ', ' Hooks Class Initialized '); } function _initialize () {//Initialize hook//Determine if Hook//include (hook.php) is turned on in config.php, reference the hook array defined in the file to $this->hooks//$t  his->enable = TRUE} function _call_hook ($which = ")//pre_system {//with Pre_system Hook Point as an example, when _call_hook (' Pre_system ') is called Make sure $this->enable = TRUE && defines $this->hooks[' Pre_system ')//if the two-dimensional array is traversed, _run_hook ($this->hooks[' Pre_system '] [$val])//If it is a one-dimensional array, then direct _run_hook ($this->hooks[' Pre_system ')} function _run_hook ($data)//$data is the array of hooks passed over {//$data we're in apppath/conf.ig/hook.php definition of the hook array//$hook [' pre_controller '] = array (//' class ' = ' MyClass ',//' function ' = ' Myfunction ',  ' filename ' = ' myclass.php ',//' filepath ' = ' hooks ',//' params ' = = Array (' Beer ', ' wine ', ' snacks ')//  );  Take out the data inside, load the APPPATH. $data [' filepath ']. $data [' filename ']; Instantiate the Hook class and invoke function.  The application to the example is $this->in_process = TRUE;  $Hook = new MyClass ();  $Hook->myfunction ($params);  $this->in_process = FALSE; }}?>

Hook points can hang multiple hooks, so when we want to extend CI, we just need to put the hook file into the AppPath folder and then declare the information of the defined hook in the apppath/config/hook.php. When the system runs to the hook point, it automatically invokes the declared hook.

This allows for scalability

More interested in CodeIgniter related content readers can view this site topic: "CodeIgniter Introductory Tutorial", "CI (codeigniter) Framework Advanced Tutorial", "PHP Excellent Development Framework Summary", "thinkphp Getting Started", " Summary of common methods of thinkphp, "Introduction to Zend Framework Frame", "Introduction to PHP Object-oriented Programming", "Introduction to Php+mysql Database Operation" and "PHP common database Operation Skills Summary"

It is hoped that this article is helpful to the PHP program design based on CodeIgniter framework.

http://www.bkjia.com/PHPjc/1127853.html www.bkjia.com true http://www.bkjia.com/PHPjc/1127853.html techarticle CI Framework Source code interpretation of the use of hook.php file to complete the function extension method, this paper describes the CI Framework source code interpretation of the use of hook.php file to complete the function extension method. Share to everyone ...

  • 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.