排序
與預期相反,返回的 ReflectionType 物件陣列不會按照原始碼中宣告類型的順序排序。
- 類別、介面、特性、iterable(由 Traversable 取代)、ReflectionIntersectionType 物件、parent 和 self:這些類型將會先返回,順序與它們宣告的順序相同。
- static 和所有內建類型(iterable 由 array 取代)將會在之後返回。它們將會永遠按照以下順序返回:static、callable、array、string、int、float、bool(或 false 或 true)、null。
請注意,當在聯集類型中使用時,iterable 是 Traversable|array 的別名。ReflectionUnionType::getTypes 將會返回這兩個 ReflectionNamedType 物件,而不是單一個名為 'iterable' 的物件。
範例
<?php
類別 PC {}
類別 C 繼承 PC {
函式 f(): null|bool|float|int|parent|PC|string|iterable|(ReflectionClass&ReflectionProperty)|callable|static|self|C {}
}
echo join(', ', array_map(
函式($t) { return ($t instanceof ReflectionIntersectionType) ? '<intersection>' : $t->getName(); },
(new ReflectionMethod('C', 'f'))->getReturnType()->getTypes()
)) . "\n";
?>
將顯示
parent, PC, Traversable, <intersection>, self, C, static, callable, array, string, int, float, bool, null
試看看: https://onlinephp.io/c/777c6