Table of Contents
CI framework source code interpretation uses the Hook.php file to complete function expansion.
Home Backend Development PHP Tutorial Interpretation of CI framework source code: How to use Hook.php file to complete function expansion, _PHP tutorial

Interpretation of CI framework source code: How to use Hook.php file to complete function expansion, _PHP tutorial

Jul 12, 2016 am 08:52 AM
ci framework hook Expand

CI framework source code interpretation uses the Hook.php file to complete function expansion.

This article explains the CI framework source code interpretation and uses the Hook.php file to complete function expansion. . Share it with everyone for your reference, the details are as follows:

After reading the source code of hook.php, you will know the principle of CI using hooks to expand.

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

The use of hooks in CI goes through a process: opening the hook, defining the hook, calling the hook, and executing the hook.

The manual has already informed the methods of opening, defining and calling. So what is the implementation principle of hook?

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Hooks {
 var $enabled = FALSE; 
 //开启hook的标志,默认是关闭的。APPPATH/config/config.php中的配置也是默认关闭的,如果想使用hook,要在config.php中开启。
 var $hooks = array();
 //在_initialize()函数初始化的过程中将APPPATH/config/hook.php中定义的hook数组,引用到$this->hooks;
 var $in_progress  = FALSE;
 //当一个hook执行的时候,会给标记 $in_process = TRUE ,是为了防止同一个hook被同时调用。
 function __construct()
 {
  $this->_initialize();
  log_message('debug', "Hooks Class Initialized");
 }
 function _initialize()
 {
  //初始化hook
  //判断config.php中是否开启hook
  //include(hook.php),将文件里定义的hook数组引用到$this->hooks
  //$this->enable = TRUE 
 }
 function _call_hook($which = '')//pre_system
 {
  //以pre_system挂钩点为例,当调用_call_hook('pre_system')时
  //确保$this->enable = TRUE && 定义了$this->hooks['pre_system']
  //如果是二维数组就遍历,依次_run_hook($this->hooks['pre_system'][$val])
  //如果是一维数组,那么直接_run_hook($this->hooks['pre_system'])
 }
 function _run_hook($data) //$data 是传递过来的hook数组
 {
  //$data 就是我们在APPPATH/config/hook.php 定义的hook数组
  //$hook['pre_controller'] = array(
  // 'class'  => 'MyClass',
  // 'function' => 'Myfunction',
  // 'filename' => 'Myclass.php',
  // 'filepath' => 'hooks',
  // 'params'  => array('beer', 'wine', 'snacks')
  // );
  //取出data里面的数据,加载
  APPPATH.$data['filepath'].$data['filename'];
  //实例化钩子类,调用function。应用到示例中就是
  $this->in_process = TRUE;
  $Hook = new MyClass();
  $Hook->Myfunction($params);
  $this->in_process = FALSE;
  }
}
?>

Copy after login

The hook point can hang multiple hooks, so when we want to expand ci, we only need to put the hook file in the APPPATH folder, and then declare the defined hook information in APPPATH/config/hook.php. Can. Then when the system reaches the hook point, it will automatically call the declared hook.

This enables scalability

Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php excellent development framework summary", "ThinkPHP introductory tutorial", "Summary of Common Methods in ThinkPHP", "Introduction Tutorial on Zend FrameWork Framework", "Introduction Tutorial on PHP Object-Oriented Programming", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127853.htmlTechArticleCI framework source code interpretation: How to use the Hook.php file to complete function expansion. This article explains the CI framework source code interpretation with examples How to use Hook.php file to complete function expansion. Share with everyone...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP's SNMP extension? How to use PHP's SNMP extension? Jun 02, 2023 am 10:22 AM

How to use PHP's SNMP extension?

Extensions and third-party modules for PHP functions Extensions and third-party modules for PHP functions Apr 13, 2024 pm 02:12 PM

Extensions and third-party modules for PHP functions

How to use CI framework in php? How to use CI framework in php? Jun 01, 2023 am 08:48 AM

How to use CI framework in php?

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL to make HTTP requests

How to install mbstring extension under CENTOS7? How to install mbstring extension under CENTOS7? Jan 06, 2024 pm 09:59 PM

How to install mbstring extension under CENTOS7?

How to use the Aurora Push extension to implement batch message push function in PHP applications How to use the Aurora Push extension to implement batch message push function in PHP applications Jul 25, 2023 pm 08:07 PM

How to use the Aurora Push extension to implement batch message push function in PHP applications

Reasons and solutions why D drive cannot be expanded in win11 system Reasons and solutions why D drive cannot be expanded in win11 system Jan 08, 2024 pm 12:30 PM

Reasons and solutions why D drive cannot be expanded in win11 system

How to use PHP's ZipArchive extension? How to use PHP's ZipArchive extension? Jun 02, 2023 am 08:13 AM

How to use PHP's ZipArchive extension?

See all articles