Php,如何获取源代码中EXCATLY方法的参数/参数?

时间:2014-10-12 13:55:17

标签: php

我正在寻找通过反射检索方法参数的方法。但是我需要它们完全是如何编写的,所以不在数组中

编辑:好的,我试过了:

public function myParams (array &arg)

$ref = new ReflectionMethod ('MyClass', 'myParams');
$ref->getParameters()

这有点回归:

array (size=1)
  0 => &
    object(ReflectionParameter)[27]
      public 'name' => string 'myParams' (length=5)

并注意到&参数中的字符?如果我删除它,我得到相同的结果。我只想获得一个array &arg字符串

1 个答案:

答案 0 :(得分:1)

array是一种原始类型,这就是为什么你没有得到开箱即用的类型提示。但你仍然可以手动检查。

class B {}
class A { public function myParams (array &$arg, B $b, $i = 0, $c) {} }

$ref = new ReflectionMethod ('A', 'myParams');
$params = $ref->getParameters();

foreach($params as $param)
{
    if($param->isArray())
    {
        echo "{$param->name} is of type Array.\n";
    }

    if($param->isPassedByReference())
    {
        echo "{$param->name} is passed by reference.\n";
    }

    if($param->getClass() !== null)
    {
        echo "{$param->name} is of class {$param->getClass()->name}.\n";
    }

    if($param->isDefaultValueAvailable())
    {
        echo "{$param->name} has a default value of {$param->getDefaultValue()}.\n";
    }

    if(!$param->isArray() && !$param->getClass() && !$param->isDefaultValueAvailable())
    {
        echo "{$param->name} is of unknonwn type.\n";
    }
}

输出:

arg is of type Array.  
arg is passed by reference.  
b is of class B. 
i has a default value of 0.
c is of unknonwn type.