PHP Conference Japan 2024

ReflectionParameter::isArray

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

ReflectionParameter::isArray檢查參數是否預期為陣列

警告

此函式已於 PHP 8.0.0 起_棄用_。強烈建議不要依賴此函式。

請參閱下面的範例,了解取得此資訊的替代方法。

說明

#[\Deprecated]
public ReflectionParameter::isArray(): bool

檢查參數是否預期為陣列。

參數

此函數沒有參數。

返回值

如果預期為 陣列,則返回 true,否則返回 false

更新日誌

版本 說明
8.0.0 此函數已被棄用,建議改用 ReflectionParameter::getType()

範例

範例 #1 PHP 8.0.0 的等效寫法

從 PHP 8.0.0 開始,以下程式碼將會報告類型是否宣告了陣列,包含作為聯集的一部分。

<?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));
}
?>

另請參閱

新增註記

使用者貢獻的註記

此頁面沒有使用者貢獻的註記。
To Top