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]

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to