note 128871 added to reflectionuniontype.gettypes
| From: | baptiste at pillot dot fr | Date: | Fri, 08 Sep 2023 12:12:12 +0000 |
| Subject: | note 128871 added to reflectionuniontype.gettypes | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220832@lists.php.net to get a copy of this message | ||
Sorting :
Contrary to what one might expect, the returned array of ReflectionType objects will not be sorted
in the same order than the types declared into the source code.
- Classes, interfaces, traits, iterable (replaced by Traversable), ReflectionIntersectionType
objects, parent and self: these types will be returned first, in the order in which they were
declared.
- static and all built-in types (iterable replaced by array) will come next. They will always be
returned in this order: static, callable, array, string, int, float, bool (or false or true), null.
Notice that when used into an union type, iterable is an alias for Traversable|array.
ReflectionUnionType::getTypes will return these both ReflectionNamedType objects, instead of a
single one named 'iterable'.
Example :
<?php
class PC {}
class C extends PC {
function f():
null|bool|float|int|parent|PC|string|iterable|(ReflectionClass&ReflectionProperty)|callable|static|self|C
{}
}
echo join(', ', array_map(
function($t) { return ($t instanceof ReflectionIntersectionType) ? '<intersection>'
: $t->getName(); },
(new ReflectionMethod('C', 'f'))->getReturnType()->getTypes()
)) . "\n";
?>
Will display :
parent, PC, Traversable, <intersection>, self, C, static, callable, array, string, int, float,
bool, null
Try it: https://onlinephp.io/c/777c6
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.157.134 (proxied: 2a01:e0a:9fc:9400:d6de:c649:55bc:3422)
----
Manual Page -- https://php.net/manual/en/reflectionuniontype.gettypes.php
Edit -- https://main.php.net/note/edit/128871
Del: integrated -- https://main.php.net/note/delete/128871/integrated
Del: useless -- https://main.php.net/note/delete/128871/useless
Del: bad code -- https://main.php.net/note/delete/128871/bad+code
Del: spam -- https://main.php.net/note/delete/128871/spam
Del: non-english -- https://main.php.net/note/delete/128871/non-english
Del: in docs -- https://main.php.net/note/delete/128871/in+docs
Del: other reasons-- https://main.php.net/note/delete/128871
Reject -- https://main.php.net/note/reject/128871
Search -- https://main.php.net/manage/user-notes.php