PHP Velho Oeste 2024

ReflectionParameter::isArray

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

ReflectionParameter::isArray配列を受け取るパラメータであるかどうかを調べる

警告

この関数は PHP 8.0.0 で 非推奨になります。この関数に頼らないことを強く推奨します。

配列を受け取るパラメータかどうかを調べる別の方法については、下に示す例を参照ください。

説明

public ReflectionParameter::isArray(): bool

配列を受け取るパラメータであるかどうかを調べます。

パラメータ

この関数にはパラメータはありません。

戻り値

配列を受け取るパラメータである場合に true、それ以外の場合に false を返します。

例1 PHP 8.0.0 以降で同等のことを行うには

PHP 8.0.0 以降では、以下のコードは 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