顯然,這不會對許多內部函式(例如 array_map 和 array_walk)的回呼參數返回 true。
(PHP 5 >= 5.4.0, PHP 7, PHP 8)
ReflectionParameter::isCallable — 返回參數是否必須為可呼叫的
自 PHP 8.0.0 起,此函式已過時。強烈建議不要依賴此函式。
請參閱下面的範例,以取得替代方法來取得此資訊。
此函式沒有參數。
版本 | 說明 |
---|---|
8.0.0 | 此函式已被棄用,改用 ReflectionParameter::getType()。 |
範例 #1 PHP 8.0.0 等效程式碼
自 PHP 8.0.0 起,以下程式碼將報告型別是否支援可呼叫的,包括作為聯集的一部分。
<?php
function declaresCallable(ReflectionParameter $reflectionParameter): bool
{
$reflectionType = $reflectionParameter->getType();
if (!$reflectionType) return false;
$types = $reflectionType instanceof ReflectionUnionType
? $reflectionType->getTypes()
: [$reflectionType];
return in_array('callable', array_map(fn(ReflectionNamedType $t) => $t->getName(), $types));
}
?>