From cdbb245b3038cb6b4f3492ebd6514f3121f0f083 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 4 Feb 2016 12:56:03 +0800 Subject: [PATCH] =?UTF-8?q?App=E7=B1=BB=E7=9A=84invokeMethod=E5=92=8Cinvok?= =?UTF-8?q?eFunction=E6=96=B9=E6=B3=95=E5=8F=98=E4=B8=BApublic=20=E5=B9=B6?= =?UTF-8?q?=E4=B8=94bindParams=E6=96=B9=E6=B3=95=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E6=8C=89=E9=A1=BA=E5=BA=8F=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/App.php | 11 ++++++++--- library/think/Loader.php | 13 +++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/library/think/App.php b/library/think/App.php index 136d6930..2fe56c13 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -120,7 +120,7 @@ class App } // 执行函数或者闭包方法 支持参数调用 - private static function invokeFunction($function, $vars = []) + public static function invokeFunction($function, $vars = []) { $reflect = new \ReflectionFunction($function); $args = self::bindParams($reflect, $vars); @@ -130,7 +130,7 @@ class App } // 调用反射执行类的方法 支持参数绑定 - private static function invokeMethod($method, $vars = []) + public static function invokeMethod($method, $vars = []) { if (empty($vars)) { // 自动获取请求变量 @@ -162,11 +162,16 @@ class App private static function bindParams($reflect, $vars) { $args = []; + // 判断数组类型 数字数组时按顺序绑定参数 + $keys = array_keys($vars); + $type = array_keys($keys) === $keys ? 1 : 0; if ($reflect->getNumberOfParameters() > 0) { $params = $reflect->getParameters(); foreach ($params as $param) { $name = $param->getName(); - if (isset($vars[$name])) { + if (1 == $type && !empty($vars)) { + $args[] = array_shift($vars); + } elseif (0 == $type && isset($vars[$name])) { $args[] = $vars[$name]; } elseif ($param->isDefaultValueAvailable()) { $args[] = $param->getDefaultValue(); diff --git a/library/think/Loader.php b/library/think/Loader.php index 4e6c8ba4..be74b333 100644 --- a/library/think/Loader.php +++ b/library/think/Loader.php @@ -357,13 +357,14 @@ class Loader $module = '.' != $info['dirname'] ? $info['dirname'] : CONTROLLER_NAME; $class = self::controller($module, $layer); if ($class) { - if (is_string($vars)) { - parse_str($vars, $vars); + if (is_scalar($vars)) { + if (strpos($vars, '=')) { + parse_str($vars, $vars); + } else { + $vars = [$vars]; + } } - $method = new \ReflectionMethod($class, $action . Config::get('action_suffix')); - // 记录执行信息 - APP_DEBUG && Log::record('[ RUN ] ' . $method->getFileName(), 'info'); - return $method->invokeArgs($class, $vars); + return App::invokeMethod([$class, $action . Config::get('action_suffix')], $vars); } } /**