Php 反思方法';s get type正在重新调整空对象
因此,我尝试获取方法的类型,以实例化类,例如:Php 反思方法';s get type正在重新调整空对象,php,Php,因此,我尝试获取方法的类型,以实例化类,例如: class MyController { public function page(AnotherClass $class) { $class->intro; } } 我有一个名为mycontroller的类和一个名为page的简单方法,它有一个类类型提示,例如: class MyController { public function page(AnotherClass $class)
class MyController
{
public function page(AnotherClass $class)
{
$class->intro;
}
}
我有一个名为mycontroller
的类和一个名为page
的简单方法,它有一个类类型提示,例如:
class MyController
{
public function page(AnotherClass $class)
{
$class->intro;
}
}
我还有另一个类,略称为另一个类
(我知道非常新颖)
好的,这就是基础,现在我尝试获取mycontroller
methodarguments页面的类型:anotherclass
您可以在下面看到我的代码的逻辑:
Class Route
{
/**
* Method paramaters
*
* @var array
*/
protected $params;
/**
* The class and method
*
* @var array
*/
protected $action;
/**
* Get the paramaters of a callable function
*
* @return void
*/
public function getParams()
{
$this->params = (new ReflectionMethod($this->action[0], $this->action[1]))->getParameters();
}
/**
* Seperate the class and method
*
* @param [type] $action
* @return void
*/
public function getClassAndMethod($action = null)
{
$this->action = explode("@", $action);
}
/**
* A get request
*
* @param string $route
* @return self
*/
public function get($route = null)
{
if(is_null($route)) {
throw new Exception("the [$route] must be defined");
}
return $this;
}
public function uses($action = null)
{
if(is_null($action)){
throw new Exception("the [$action] must be set");
}
if(is_callable($action)){
return call_user_func($action);
}
// Get the action
$this->getClassAndMethod($action);
// Get the params of the method
$this->getParams();
foreach ($this->params as $param) {
print_R($param->getType());
}
// var_dump($action[0]);
}
}
这就是所谓的:
echo (new Route)->get('hello')->uses('MyController@page');
因此,上面所做的是,它通过@
符号拆分uses方法参数,[0]
将是类,[1]
将是类的方法,然后我简单地ReflectionMethod
来获取所述方法的参数,然后我尝试获取参数类型,这就是我一直坚持的,因为它只是不断返回一个空对象:
ReflectionNamedType对象{)
因此,我的问题是,为什么它返回一个空对象,以及如何获取参数的类型?您必须
echo
而不是print\r
:
foreach ($this->params as $param) {
echo $param->getType() ; //AnotherClass
}
因为使用\uuu toString()
来显示它
或
foreach ($this->params as $param) {
print_r($param->getClass()) ; //AnotherClass
}