PHP Velho Oeste 2024

ReflectionParameter::isArray

(PHP 5 >= 5.1.2, PHP 7, PHP 8)

ReflectionParameter::isArrayChecks if parameter expects an array

警告

本函数已自 PHP 8.0.0 起被废弃。强烈建议不要依赖本函数。

See the example below for an alternative way to derive this information.

说明

public ReflectionParameter::isArray(): bool

Checks if the parameter expects an array.

参数

此函数没有参数。

返回值

true if an array is expected, false otherwise.

示例

示例 #1 PHP 8.0.0 equivalent

As of PHP 8.0.0, the following code will report if a type declares arrays, including as part of a union.

<?php
function declaresArray(ReflectionParameter $reflectionParameter): bool
{
$reflectionType = $reflectionParameter->getType();

if (!
$reflectionType) return false;

$types = $reflectionType instanceof ReflectionUnionType
? $reflectionType->getTypes()
: [
$reflectionType];

return
in_array('array', array_map(fn(ReflectionNamedType $t) => $t->getName(), $types));
}
?>

参见

add a note add a note

User Contributed Notes 1 note

up
1
denis at example dot com
4 years ago
Hi, this is simple example of how to use it.

class Test
{
    public function testArray(array $a)
    {
        // do something...
       
        return $a;
    }
   
    public function testString(string $a)
    {
        // do something...
       
        return $a;
    }
}

$reflection = new ReflectionClass('Test');

foreach($reflection->getMethods() as $methods){
    foreach($methods->getParameters() as $param){
        var_dump($param->isArray());
    }
}

//bool(true)
//bool(false)
To Top