CodeIgniter框架鉤子機(jī)制實(shí)現(xiàn)方法【hooks類】
本文實(shí)例講述了CodeIgniter框架鉤子機(jī)制實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
記得上一次去到喜啦面試,面試官問我一個(gè)問題:codeigniter是如何實(shí)現(xiàn)鉤子機(jī)制的?
當(dāng)時(shí)答不上來,后來回來之后查了一些資料才明白,所以在這里記錄一下:
codeigniter的鉤子是這樣實(shí)現(xiàn)的:首先在框架的核心文件system/core/CodeIniter.php文件的 122行,載入Hooks類,接著在該文件中定義了幾個(gè)掛載點(diǎn),比如pre_system
(129行)、post_controller_constructor
(295行)等,并在這些掛載點(diǎn)上面執(zhí)行hooks類的_call_hook()
方法。
另附codeigniter的hooks類的源代碼:
<?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 EllisLab Dev Team * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) * @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. * * @package CodeIgniter * @subpackage Libraries * @category Libraries * @author EllisLab 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 if ($CFG->item('enable_hooks') == FALSE) { return; } // Grab the "hooks" definition file. // If there are no hooks, we're done. 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; } $this->hooks =& $hook; $this->enabled = TRUE; } // -------------------------------------------------------------------- /** * Call Hook * * Calls a particular hook * * @access private * @param string the hook name * @return mixed */ function _call_hook($which = '') { 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) { 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 if ($this->in_progress == TRUE) { return; } // ----------------------------------- // 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 = ''; 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 // ----------------------------------- $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); } $this->in_progress = FALSE; return TRUE; } } // END CI_Hooks class /* End of file Hooks.php */ /* Location: ./system/core/Hooks.php */
可以看出codeigniter實(shí)現(xiàn)鉤子機(jī)制的方式不夠優(yōu)雅,其實(shí)完全可以使用觀察者模式來實(shí)現(xiàn)鉤子機(jī)制,將掛載點(diǎn)當(dāng)做監(jiān)聽的事件。
更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
SQL+HTML+PHP?一個(gè)簡單論壇網(wǎng)站的綜合開發(fā)案例(注冊、登錄、注銷、修改信息、留言等)
這篇文章主要介紹了SQL+HTML+PHP?一個(gè)簡單論壇網(wǎng)站的綜合開發(fā)案例(注冊、登錄、注銷、修改信息、留言等),需要的朋友可以參考下2022-12-12PHP利用pdo_odbc實(shí)現(xiàn)連接數(shù)據(jù)庫示例【基于ThinkPHP5.1搭建的項(xiàng)目】
這篇文章主要介紹了PHP利用pdo_odbc實(shí)現(xiàn)連接數(shù)據(jù)庫,結(jié)合實(shí)例形式分析了基于ThinkPHP5.1框架使用pdo_odbc連接數(shù)據(jù)庫相關(guān)操作步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05Laravel統(tǒng)一錯(cuò)誤處理為JSON的方法介紹
這篇文章主要給大家介紹了關(guān)于Laravel統(tǒng)一錯(cuò)誤處理為JSON的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10一個(gè)簡單的PHP驗(yàn)證碼實(shí)現(xiàn)代碼
為提高網(wǎng)站安全性,登陸采用驗(yàn)證碼是必不可少的。一款簡單精致的PHP驗(yàn)證碼應(yīng)運(yùn)而生!此驗(yàn)證碼簡潔美觀,源碼簡單,可以自定義修改樣式,是一款不錯(cuò)的驗(yàn)證碼。2014-05-05destoon實(shí)現(xiàn)VIP排名一直在前面排序的方法
這篇文章主要介紹了destoon實(shí)現(xiàn)VIP排名一直在前面排序的方法,在destoon開發(fā)中非常實(shí)用,需要的朋友可以參考下2014-08-08