相关疑难解决方法(0)

PHP反射 - 获取方法参数类型为字符串

我正在尝试使用PHP反射根据控制器方法中的参数类型自动动态加载模型的类文件.这是一个示例控制器方法.

<?php

class ExampleController
{
    public function PostMaterial(SteelSlugModel $model)
    {
        //etc...
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所拥有的.

//Target the first parameter, as an example
$param = new ReflectionParameter(array('ExampleController', 'PostMaterial'), 0);

//Echo the type of the parameter
echo $param->getClass()->name;
Run Code Online (Sandbox Code Playgroud)

这是有效的,输出将是'SteelSlugModel',如预期的那样.但是,有可能模型的类文件可能尚未加载,并且使用getClass()要求定义类 - 为什么我这样做的一部分是自动加载控制器操作可能需要的任何模型.

有没有办法获取参数类型的名称,而无需先加载类文件?

php reflection

12
推荐指数
3
解决办法
9091
查看次数

标签 统计

php ×1

reflection ×1