This article describes the CI Framework source code interpretation of the use of hook.php file to complete the function extension method. Share to everyone for your reference, specific as follows:
See the source code of hook.php, you know CI use hook to expand the principle.
The basic knowledge of hooks http://codeigniter.org.cn/user_guide/general/hooks.html
The use of hooks in CI has gone through a process of opening hooks, defining hooks, calling hooks, and executing hooks.
The manual tells you how to open, define, and invoke methods. 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 sign of the hook is turned on 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 the $this->hooks;
var $in _progress = FALSE;
When a hook executes, 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///Judge config.php to open Hook//include (hook.php), referencing the hook array defined in the file to $this->hooks $this->enable = TRUE} function _call_hook ($which = "")//pre_system {//to Pre_system Hook Point as an example when invoking _call_hook (' Pre_sy Stem ')//ensures $this->enable = TRUE && defines $this->hooks[' Pre_system '//If a two-dimensional array is traversed, sequentially _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 an array of hooks passed over {//$data is the hook array we defined in apppath/config/hook.php//$hook [' pre_controller '] = array (//' class ' => ' MyClass '), ' function ' => ' myfunction ',//' filename ' => ' myclass.php ',//' filepath ' => ' hooks ',//' params ' =>
Array (' beer ', ' wine ', ' snacks ')//);
Take the data inside and load the AppPath. $data [' filepath ']. $data [' filename ']; Instantiate the Hook class and invoke function.
Applied 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 expand CI, we just need to put the hook file under the AppPath folder and then go to the apppath/config/hook.php to declare the defined hook information. Then when the system runs to the hook point, the declared hook is automatically invoked.
This enables scalability
More interested in CodeIgniter related content readers can view the site topics: "CodeIgniter Introductory Course", "CI (CodeIgniter) Framework Advanced Course", "PHP Excellent Development Framework Summary", "thinkphp Introductory Course", " Thinkphp Common Methods Summary, "Zend Framework Introduction Course", "PHP object-oriented Programming Introduction Course", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on CodeIgniter framework.