CI框架源码阅读---------钩子类hooks.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * CodeIgniter * * An open source application development framework for PHP 5.1.6 or newer * * @package CodeIgniter * @author ExpressionEngine Dev Team * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. * @license http://codeigniter.com/user_guide/license.html * @link http://codeigniter.com * @since Version 1.0 * @filesource */ // ------------------------------------ /** * CodeIgniter Hooks Class * * Provides 提供 a mechanism 机制 to extend the base system without hacking. * 用户手册地址:http://codeigniter.org.cn/user_guide/general/hooks.html * @package CodeIgniter * @subpackage Libraries * @category Libraries * @author ExpressionEngine Dev Team * @link http://codeigniter.com/user_guide/libraries/encryption.html */ class CI_Hooks { /** * Determines wether hooks are enabled * 决定钩子是否启用 * * @var bool */ var $enabled = FALSE; /** * List of all hooks set in config/hooks.php * * @var array */ var $hooks = array(); /** * Determines wether hook is in progress, used to prevent 防止 infinte 无限 loops * * @var bool */ var $in_progress = FALSE; /** * Constructor * */ function __construct() { $this->_initialize(); log_message('debug', "Hooks Class Initialized"); } // -------------------------------- /** * Initialize the Hooks Preferences 参数,首选项 * 初始化钩子 * @access private * @return void */ function _initialize() { $CFG =& load_class('Config', 'core'); // If hooks are not enabled in the config file // there is nothing else to do // 如果配置文件中设置了是不允许hooks,则直接返回退出本函数。 if ($CFG->item('enable_hooks') == FALSE) { return; } // Grab the "hooks" definition file. // 抓取钩子的定义文件 // If there are no hooks, we're done. // 如果没有定义hooks.php没有定义$hook数组我们直接返回 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/hooks.php')) { include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'); } elseif (is_file(APPPATH.'config/hooks.php')) { include(APPPATH.'config/hooks.php'); } if ( ! isset($hook) OR ! is_array($hook)) { return; } // 将hooks.php 中的$hook数组引用到$this->hooks // 开启$this->enabled $this->hooks =& $hook; $this->enabled = TRUE; } // -------------------------------- /** * Call Hook * 外部其实就是调用这个_call_hook函数进行调用钩子程序。 * 而此方法中再调用_run_hook去执行相应的钩子。 * Calls a particular hook * * @access private * @param string the hook name * @return mixed */ function _call_hook($which = '') { // 判断$this->enabled 是否开启 和 要调用的钩子是否在$htis->hooks中存在。 if ( ! $this->enabled OR ! isset($this->hooks[$which])) { return FALSE; } // 判断要调用的钩子是否是一个二维数组,如果是就遍历执行。 // 如果不是二维数组就直接执行 // 这里说明,在一个挂钩点可以执行多个钩子,就是通过定义二维数组来实现的。 if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0])) { foreach ($this->hooks[$which] as $val) { $this->_run_hook($val); } } else { $this->_run_hook($this->hooks[$which]); } return TRUE; } // -------------------------------- /** * Run Hook * 运行钩子 * Runs a particular 特别的 hook * * @access private * @param array the hook details * @return bool */ function _run_hook($data) { /* * $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是不是数组如果不是则返回 * */ if ( ! is_array($data)) { return FALSE; } // ----------------------------------- // Safety - Prevents run-away loops // ----------------------------------- // If the script being called happens to have the same // hook call within it a loop can happen // 如果调用某一个hook,执行某些脚本,而有可能这些脚本里面再会触发其它hook // 如果这个其它hook里面又包含了当前 // 的hook,那么就会进入死循环,这个in_progress的存在就是阻止这种情况。 if ($this->in_progress == TRUE) { return; } // ----------------------------------- // 取出data里面的数据,加载 APPPATH.$data['filepath'].$data['filename']; // Set file path // ----------------------------------- if ( ! isset($data['filepath']) OR ! isset($data['filename'])) { return FALSE; } $filepath = APPPATH.$data['filepath'].'/'.$data['filename']; if ( ! file_exists($filepath)) { return FALSE; } // ----------------------------------- // Set class/function name // ----------------------------------- $class = FALSE; $function = FALSE; $params = ''; // 取出$hooks 中的class function params if (isset($data['class']) AND $data['class'] != '') { $class = $data['class']; } if (isset($data['function'])) { $function = $data['function']; } if (isset($data['params'])) { $params = $data['params']; } if ($class === FALSE AND $function === FALSE) { return FALSE; } // ----------------------------------- // Set the in_progress flag // 在开始执行钩子相应的程序之前,先把当前hook的状态设为正在运行中。 // ----------------------------------- $this->in_progress = TRUE; // ----------------------------------- // Call the requested class and/or function // 包含钩子文件并实例化类,调用函数 // ----------------------------------- if ($class !== FALSE) { if ( ! class_exists($class)) { require($filepath); } $HOOK = new $class; $HOOK->$function($params); } else { if ( ! function_exists($function)) { require($filepath); } $function($params); } // 执行相应程序完毕后,重新把当前hook的状态改为非运行中 // 以让它可以再次被触发。 $this->in_progress = FALSE; return TRUE; } } // END CI_Hooks class /* End of file Hooks.php */ /* Location: ./system/core/Hooks.php */

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。

在PHP中,trait适用于需要方法复用但不适合使用继承的情况。1)trait允许在类中复用方法,避免多重继承复杂性。2)使用trait时需注意方法冲突,可通过insteadof和as关键字解决。3)应避免过度使用trait,保持其单一职责,以优化性能和提高代码可维护性。

依赖注入容器(DIC)是一种管理和提供对象依赖关系的工具,用于PHP项目中。DIC的主要好处包括:1.解耦,使组件独立,代码易维护和测试;2.灵活性,易替换或修改依赖关系;3.可测试性,方便注入mock对象进行单元测试。

SplFixedArray在PHP中是一种固定大小的数组,适用于需要高性能和低内存使用量的场景。1)它在创建时需指定大小,避免动态调整带来的开销。2)基于C语言数组,直接操作内存,访问速度快。3)适合大规模数据处理和内存敏感环境,但需谨慎使用,因其大小固定。

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

JavaScript中处理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。1.??返回第一个非null或非undefined的操作数。2.??=将变量赋值为右操作数的值,但前提是该变量为null或undefined。这些操作符简化了代码逻辑,提高了可读性和性能。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

Dreamweaver CS6
视觉化网页开发工具