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

    版本:php7

    (PHP 7)

    Gets a parameter's type

    说明

    publicReflectionParameter::getType(void) :ReflectionType

    Gets the associated type of a parameter.

    参数

    此函数没有参数。

    返回值

    Returns aReflectionTypeobject if a parameter type is specified,NULLotherwise.

    范例

    Example #1ReflectionParameter::getType()Usage as of PHP 7.1.0

    As of PHP 7.1.0,ReflectionType::__toString()is deprecated,andReflectionParameter::getType()mayreturn an instance ofReflectionNamedType. To get the name of the parameter type,ReflectionNamedType()is available in this case.

    <?php
    function someFunction(int $param, $param2) {}
    $reflectionFunc = new ReflectionFunction('someFunction');
    $reflectionParams = $reflectionFunc->getParameters();
    $reflectionType1 = $reflectionParams[0]->getType();
    $reflectionType2 = $reflectionParams[1]->getType();
    assert($reflectionType1 instanceof ReflectionNamedType);
    echo $reflectionType1->getName(), PHP_EOL;
    var_dump($reflectionType2);
    ?>
    

    以上例程会输出:

    int
    NULL
    

    Example #2ReflectionParameter::getType()Usage before PHP 7.1.0

    <?php
    function someFunction(int $param, $param2) {}
    $reflectionFunc = new ReflectionFunction('someFunction');
    $reflectionParams = $reflectionFunc->getParameters();
    $reflectionType1 = $reflectionParams[0]->getType();
    $reflectionType2 = $reflectionParams[1]->getType();
    echo $reflectionType1, PHP_EOL;
    var_dump($reflectionType2);
    ?>
    

    Output of the above example in PHP 7.0:

    int
    NULL
    

    参见

    • ReflectionParameter::hasType() Checks if parameter has a type
    • ReflectionType::__toString() To string