Re: [RFC][Vote] ReflectionType Improvements
| From: | Rowan Collins | Date: | Tue, 05 Jul 2016 14:28:08 +0000 |
| Subject: | Re: [RFC][Vote] ReflectionType Improvements | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-94386@lists.php.net to get a copy of this message | ||
On 05/07/2016 14:21, Levi Morrison wrote:
if ($type instanceof ReflectionClassType) {
$r = $type->getClass();
}
The (currently) correct code would be:
if (class_exists($type->getName()) || interface_exists($type->getName())) {
$r = new ReflectionClass($type->getName());
}
This seems to be combining two checks into one:
- is this a "simple / builtin type" or a "class / interface type"?
- is the class / interface referenced currently defined or autoloadable?
The first question can be answered with the existing ReflectionType::isBuiltin() method. The fact that the second is currently difficult to answer doesn't seem to have anything to do with reflecting type hints.
It seems like if there was a use case for "is this type hint a currently defined or autoloadable class / interface?" it would ideally be written something like this:
if ( ! $type->isBuiltin() && object_type_exists($type->getName()) )
The term "object type" being bikesheddable as a way of encompassing your hypothetical enums or other future "class-like" types. Of course, the fact that "class_exists" and "ReflectionClass" use different definitions of "class" already is somewhat awkward...
Or if the aim is to simplify the reflection usage, why require the if statement at all:
try {
$r = $type->getReflectionClass();} catch ( ReflectionException $e ) {
// type is builtin or refers to an undefined class} Regards, -- Rowan Collins [IMSoP]