core/Route.php
public static function runMethod(string $class, string $method)
    {
        $oReflectionClass = new \ReflectionClass($class);
        //获取方法信息
        $method = $oReflectionClass->getMethod($method);
        if (!$method->isPublic()) {
            throw new RouteError('未公开方法:' . $method);
        }
        //获取方法参数
        $params = $method->getParameters();
        $args = [];
        if (count($params) > 0) {
            foreach ($params as $param) {
                //获取变量名称
                $name = $param->getName();
                $type = 'any';
                //如果可以获取类型
                if (is_callable([$param, 'hasType'])) {
                    if ($param->hasType()) {
                        $refType = $param->getType();
                        if ($refType != null) {
                            if (is_callable([$refType, 'getName'])) {
                                $type = $refType->getName();
                            } else {
                                $type = strval($refType);
                            }
                            $type = empty($type) ? 'any' : $type;
                        }
                    }
                }
                //类型获取不到
                if ($type == 'any') {
                    if (is_callable([$param, 'getClass'])) {
                        $refType = $param->getClass();
                        if ($refType != null) {
                            if (is_callable([$refType, 'getName'])) {
                                $type = $refType->getName();
                            } else {
                                $type = strval($refType);
                            }
                            $type = empty($type) ? 'any' : $type;
                        }
                    }
                }
                //默认值
                $def = null;
                //如果有默认值,从默认值中获取类型
                if ($param->isOptional()) {
                    $def = $param->getDefaultValue();
                    if ($type == 'any') {
                        $type = gettype($def);
                    }
                }
                switch ($type) {
                    case 'bool':
                    case 'boolean':
                        $args[] = Request::param($name . ':b', $def);
                        break;
                    case 'int':
                    case 'integer':
                        $val = Request::param($name . ':s', $def);
                        //如果默认值是整数,但是传递的值是浮点数
                        if (preg_match('@[+-]?\d*\.\d+@', $val)) {
                            $args[] = Request::param($name . ':f', $def);
                        } else {
                            $args[] = Request::param($name . ':i', $def);
                        }
                        break;
                    case 'double':
                    case 'float':
                        $args[] = Request::param($name . ':f', $def);
                        break;
                    case 'string':
                        $args[] = Request::param($name . ':s', $def);
                        break;
                    case 'array':
                        $args[] = Request::param($name . ':a', $def);
                        break;
                    default :
                        $args[] = Request::param($name, $def);
                        break;
                }
            }
        }
        $example = new $class();
        //如果有初始化方法
        if (method_exists($example, 'initialize')) {
            $example->initialize();
        }
        //调用方法
        $out = $method->invokeArgs($example, $args);
        if (Request::getContentType() == 'application/json' || Request::getContentType() == 'text/json') {
            die(json_encode($out, JSON_UNESCAPED_UNICODE));
        } else if (is_array($out)) {
            Request::setContentType('json');
            die(json_encode($out, JSON_UNESCAPED_UNICODE));
        } else if (!empty($out)) {
            Request::setContentType('html');
            echo $out;
        }
    }
public static function runMethod(string $class, string $method)
    {
        $oReflectionClass = new \ReflectionClass($class);
        //获取方法信息
        $method = $oReflectionClass->getMethod($method);
        if (!$method->isPublic()) {
            throw new RouteError('未公开方法:' . $method);
        }
        //获取方法参数
        $params = $method->getParameters();
        $args = [];
        if (count($params) > 0) {
            foreach ($params as $param) {
                //获取变量名称
                $name = $param->getName();
                $type = 'any';
                //如果可以获取类型
                if (is_callable([$param, 'hasType'])) {
                    if ($param->hasType()) {
                        $refType = $param->getType();
                        if ($refType != null) {
                            if (is_callable([$refType, 'getName'])) {
                                $type = $refType->getName();
                            } else {
                                $type = strval($refType);
                            }
                            $type = empty($type) ? 'any' : $type;
                        }
                    }
                }
                //类型获取不到
                if ($type == 'any') {
                    if (is_callable([$param, 'getClass'])) {
                        $refType = $param->getClass();
                        if ($refType != null) {
                            if (is_callable([$refType, 'getName'])) {
                                $type = $refType->getName();
                            } else {
                                $type = strval($refType);
                            }
                            $type = empty($type) ? 'any' : $type;
                        }
                    }
                }
                //默认值
                $def = null;
                //如果有默认值,从默认值中获取类型
                if ($param->isOptional()) {
                    $def = $param->getDefaultValue();
                    if ($type == 'any') {
                        $type = gettype($def);
                    }
                }
                switch ($type) {
                    case 'bool':
                    case 'boolean':
                        $args[] = Request::param($name . ':b', $def);
                        break;
                    case 'int':
                    case 'integer':
                        $val = Request::param($name . ':s', $def);
                        //如果默认值是整数,但是传递的值是浮点数
                        if (preg_match('@[+-]?\d*\.\d+@', $val)) {
                            $args[] = Request::param($name . ':f', $def);
                        } else {
                            $args[] = Request::param($name . ':i', $def);
                        }
                        break;
                    case 'double':
                    case 'float':
                        $args[] = Request::param($name . ':f', $def);
                        break;
                    case 'string':
                        $args[] = Request::param($name . ':s', $def);
                        break;
                    case 'array':
                        $args[] = Request::param($name . ':a', $def);
                        break;
                    default :
                        $args[] = Request::param($name, $def);
                        break;
                }
            }
        }
        $example = new $class();
        //如果有初始化方法
        if (method_exists($example, 'initialize')) {
            $example->initialize();
        }
        //调用方法
        $out = $method->invokeArgs($example, $args);
        if (Request::getContentType() == 'application/json' || Request::getContentType() == 'text/json') {
            die(json_encode($out, JSON_UNESCAPED_UNICODE));
        } else if (is_array($out)) {
            Request::setContentType('json');
            die(json_encode($out, JSON_UNESCAPED_UNICODE));
        } else if (!empty($out)) {
            Request::setContentType('html');
            echo $out;
        }
    }
运行实例的方法
@param string $class
@param string $method
@throws RouteError
@throws \ReflectionException