PHP反射(ReflectionClass、ReflectionMethod)在ThinkPHP框架的控制器排程模組中的應用
阿新 • • 發佈:2019-01-27
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) 帶引數執行方法
例項演示:
【另外一段參考程式碼】<?php class 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!'; }
<?php class 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!'; }