• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • ReflectionParameter::getClass()

    (PHP 5, PHP 7)

    获得类型提示类。

    说明

    publicReflectionParameter::getClass(void) :ReflectionClass

    获取参数的类型提示类,类型为ReflectionClass对象。

    Warning

    本函数还未编写文档,仅有参数列表。

    参数

    此函数没有参数。

    返回值

    一个ReflectionClass对象。

    范例

    使用ReflectionParameter类

    <?php
    function foo(Exception $a) { }
    $functionReflection = new ReflectionFunction('foo');
    $parameters = $functionReflection->getParameters();
    $aParameter = $parameters[0];
    echo $aParameter->getClass() >name;
    ?>
    

    以上例程会输出:

    Exception
    

    参见

    • ReflectionParameter::getDeclaringClass() Gets declaring class
    The method returns ReflectionClass object of parameter type class or NULL if none.
    <?php
    class A {
      function b(B $c, array $d, $e) {
      }
    }
    class B {
    }
    $refl = new ReflectionClass('A');
    $par = $refl->getMethod('b')->getParameters();
    var_dump($par[0]->getClass() >getName()); // outputs B
    var_dump($par[1]->getClass()); // note that array type outputs NULL
    var_dump($par[2]->getClass()); // outputs NULL
    ?>
    
    ReflectionParameter::getClass() will cause a fatal error (and trigger __autoload) if the class required by the parameter is not defined. 
    Sometimes it's useful to only know the class name without needing the class to be loaded.
    Here's a simple function that will retrieve only the class name without requiring the class to exist:
    <?php
    function getClassName(ReflectionParameter $param) {
      preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
      return isset($matches[1]) ? $matches[1] : null;
    }
    ?>