2017年3月18日17:22:52 php
php版本 5.6.27數組
5.3如下和5.3以上的版本在PHP類與對象區別很大,請注意函數
其實原理很簡單,有些人把事情弄的過於發雜,其實就是調用某個目錄下的好比/hook目錄下注冊在hook函數裏面和讀取hook配置文件裏面的類的方法的一個調用類的方法的功能ui
目的就是最少改動代碼,改動舊功能,或者增長一些新功能,或者簡單說成調用函數都行this
可是讀取hook的配置文件,仍是須要在系統的裏面每次都須要讀取,其實就失去了hook的意義,建議只作鉤子自己的就好url
參看一下ci的hook,僅截取hook函數核心部分spa
<?php protected function _run_hook($data) { // Closures/lambda functions and array($object, 'method') callables if (is_callable($data)) { is_array($data) ? $data[0]->{$data[1]}() : $data(); return TRUE; } elseif (!is_array($data)) { return FALSE; } if ($this->_in_progress === TRUE) { return; } if (!isset($data['filepath'], $data['filename'])) { return FALSE; } $filepath = APPPATH . $data['filepath'] . '/' . $data['filename']; if (!file_exists($filepath)) { return FALSE; } $class = empty($data['class']) ? FALSE : $data['class']; $function = empty($data['function']) ? FALSE : $data['function']; $params = isset($data['params']) ? $data['params'] : ''; if (empty($function)) { return FALSE; } // Set the _in_progress flag $this->_in_progress = TRUE; // Call the requested class and/or function if ($class !== FALSE) { // The object is stored? if (isset($this->_objects[$class])) { if (method_exists($this->_objects[$class], $function)) { $this->_objects[$class]->$function($params); } else { return $this->_in_progress = FALSE; } } else { class_exists($class, FALSE) OR require_once($filepath); if (!class_exists($class, FALSE) OR ! method_exists($class, $function)) { return $this->_in_progress = FALSE; } // Store the object and execute the method $this->_objects[$class] = new $class(); $this->_objects[$class]->$function($params);
// 核心部分 讀取參數部分,去實例化類調用方法 傳遞參數 其實這也是MVC url路由實現的核心,如今不少
//都是使用 call_user_func_array call_user_func 這兩個方法
} } else { function_exists($function) OR require_once($filepath); if (!function_exists($function)) { return $this->_in_progress = FALSE; } $function($params); } $this->_in_progress = FALSE; return TRUE; }
原理圖解3d
我的實現版本code
若是你以爲麻煩,甚至能夠寫個方法都行,建議寫成一個類,由於有些東西須要更多的信息對象
1.php
include 'hook.class.php'; $rr = new hook(); //$ee = $rr->get_all_class(); $rr->run_hook('ff','ss',array()); //echo '<pre>'; //print_r($ee); //echo '</pre>';
hook.class.php
class hook { public $HOOK_PATH; public $PATH; //完整鉤子文件目錄 public $object; //調用的時候這個類使用的時候,必須在系統的執行流程當中 public function __construct() { $this->HOOK_PATH = ''; //項目的路徑,根據你的項目計算路徑 $current_path = str_replace("\\", "/", getcwd()); //獲取當前目錄 //這個地方在實際用的時候看你須要調整 $this->PATH = $current_path . $this->HOOK_PATH; } /* 註冊鉤子 也能夠叫作運行鉤子 * $class 類名稱 * $function 方法 * $param 方法參數 */ public function run_hook($class, $function, $param = array()) { include $this->PATH . '/' . $class . '.class.php'; // var_dump($this->PATH . '/' . $class . '.class.php'); // call_user_func_array(array($class, $function), $param);//只能調用類的靜態方法 // call_user_func(array($class, $function), $param); //只能調用類的靜態方法 // 其餘寫法 $this->object = new $class(); $this->object->$function($param); //這樣就能夠不用調用靜態方法 } //返回當前已經全部的鉤子類和方法 不要當前方法調用這個核心類,須要稍微改造,在$hook_array[$key]['function']的返回方法名的時候 public function get_all_class() { //搜尋hook目錄下的全部鉤子文件,返回數組 // $this->PATH // var_dump($this->PATH); $file_array = scandir($this->PATH); $hook_array = array(); foreach ($file_array as $key => $value) { if (strpos($value, '.class.php') == true) { //掃描路徑絕對不能和這個類自己在一個同一個目錄下,否則會出現重複聲明的同名類 $name = explode('.', $value); $hook_array[$key]['name'] = $name['0'] . '鉤子類'; $hook_array[$key]['url'] = $this->PATH . '/' . $value; // include $hook_array[$key]['url']; // $cc = new $name['0'](); // $hook_array[$key]['function'][] = get_class_methods($cc);
// $hook_array[$key]['function']['param'][] = get_class_vars($class_name); //獲取方法變量
} } return $hook_array; } }
調用的某個類名
ff.class.php 的ss方法
public function ss() { // static public function ss() { echo 'dddddddddddddddddddd'; }
另外一個版本
更方便調用
class hooks { const Directory_Structure = '/hooks/'; //相對目錄的路徑 具體項目使用的時候須要調整 static public function get_path() { return str_replace("\\", "/", getcwd()); } static public function run_hook($class, $function, $param = array()) { $s = include self::get_path() . self::Directory_Structure .$class. '.class.php'; call_user_func(array($class, $function), $param); //只能調用類的靜態方法 // 其餘寫法 // $object = new $class(); // $object->$function($param); //這樣就能夠不用調用靜態方法 } }
使用
include 'hooks.class.php'; hooks::run_hook('ee', 'vv',$param =array());
固然也能夠這麼訪問
$foo = new hooks();
$foo->run_hook('ee', 'vv',array());
$foo::run_hook('ee', 'vv',array());
自 PHP 5.3.0 起,能夠用一個變量來動態調用類。但該變量的值不能爲關鍵字 self,parent 或 static。
鉤子是比較靈活的,能夠額外增長一個功能代碼,使代碼更整潔,好比在作什麼一些重要操做,建立訂單,在建立訂單以前須要作些什麼,在建立以後作些什麼,均可以使用鉤子
這樣代碼更加靈活