PHP反射(ReflectionClass、ReflectionMethod)在ThinkPHP架構的控制器調度模組中的應用

來源:互聯網
上載者:User

標籤:

ThinkPHP架構的控制器模組是如何? 前控制器、後控制器,及如何執行帶參數的方法?

PHP系統內建的 ReflectionClass、ReflectionMethod 類,可以反射使用者自訂類的中屬性,方法的許可權和參數等資訊,通過這些資訊可以準確的控制方法的執行。

ReflectionClass:  [PHP手冊]詳情

主要用的方法:

hasMethod(string)  是否存在某個方法

getMethod(string)  擷取方法

ReflectionMethod:  [PHP手冊]詳情

主要方法:

isPublic()    是否為 public 方法

getNumberOfParameters()  擷取參數個數

getParamters()  擷取參數資訊

invoke( object $object [, mixed $parameter [, mixed $... ]] ) 執行方法  

invokeArgs(object obj, array args)     帶參數執行方法

 

執行個體示範:

<?phpclass BlogAction {public function detail() {echo ‘detail‘ . "\r\n";}public function test($year = 2014, $month = 4, $day = 21) {echo $year . ‘--‘ . $month . ‘--‘ . $day . "\r\n";}public function _before_detail() {echo __FUNCTION__ . "\r\n";}public function _after_detail() {echo __FUNCTION__ . "\r\n";}}// 執行detail方法$method = new ReflectionMethod(‘BlogAction‘, ‘detail‘);$instance = new BlogAction();// 進行許可權判斷if ($method->isPublic()) {$class = new ReflectionClass(‘BlogAction‘);// 執行前置方法if ($class->hasMethod(‘_before_detail‘)) {$beforeMethod = $class->getMethod(‘_before_detail‘);if ($beforeMethod->isPublic()) {$beforeMethod->invoke($instance);}}$method->invoke(new BlogAction);// 執行後置方法if ($class->hasMethod(‘_after_detail‘)) {$beforeMethod = $class->getMethod(‘_after_detail‘);if ($beforeMethod->isPublic()) {$beforeMethod->invoke($instance);}}}// 執行帶參數的方法$method = new ReflectionMethod(‘BlogAction‘, ‘test‘);$params = $method->getParameters();foreach ($params as $param) {$paramName = $param->getName();if (isset($_REQUEST[$paramName])) {$args[] = $_REQUEST[$paramName];} elseif ($param->isDefaultValueAvailable()) {$args[] = $param->getDefaultValue();}}if (count($args) == $method->getNumberOfParameters()) {$method->invokeArgs($instance, $args);} else {echo ‘parameters is wrong!‘;}

 

 

 

 

【另外一段參考代碼】

/** * 執行App控制器 */public function execApp() {// 建立action控制器執行個體$className = MODULE_NAME . ‘Controller‘;$namespaceClassName = ‘\\apps\\‘ . APP_NAME . ‘\\controller\\‘ . $className;load_class($namespaceClassName, false);if (!class_exists($namespaceClassName)) {throw new \Exception(‘Oops! Module not found : ‘ . $namespaceClassName);}$controller = new $namespaceClassName();// 擷取當前操作名$action = ACTION_NAME;// 執行當前操作//call_user_func(array(&$controller, $action)); // 其實吧,用這個函數足夠啦!!!try {$methodInfo = new \ReflectionMethod($namespaceClassName, $action);if ($methodInfo->isPublic() && !$methodInfo->isStatic()) {$methodInfo->invoke($controller);} else { // 操作方法不是public類型,拋出異常throw new \ReflectionException();}} catch (\ReflectionException $e) {// 方法調用發生異常後,引導到__call方法處理$methodInfo = new \ReflectionMethod($namespaceClassName, ‘__call‘);$methodInfo->invokeArgs($controller, array($action, ‘‘));}return;}

PHP反射(ReflectionClass、ReflectionMethod)在ThinkPHP架構的控制器調度模組中的應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.