PHP Sadness

(<7.0) Can't inspect some kinds of arguments with reflection

As of PHP 7.0, this is now possible through ReflectionParameter::getType().

Before PHP 7.0, it is nearly impossible to determine 'SomeClass' using reflection from this prototype without also loading the class:

function example(SomeClass $obj)

This is because you have to use ReflectionParameter::getClass(), which tries to instantiate the typehint instead of returning it as a string.

ReflectionException: Class SomeClass does not exist

This makes it difficult to inspect PHP files of large systems where the inspection tool might not have the entire class hierarchy loaded but still wants to scan particular files.

One (disgusting and unacceptable) solution is to parse the output of ReflectionParameter::__toString():

$fn = new ReflectionFunction("example");
$params = $fn->getParameters();
print $params[0]->__toString();

This produces:

Parameter #0 [ <required> SomeClass $obj ]

Correctly extracting the appropriate part from this string (regardless of how it might vary with optional values, references, defaults, etc) is left as an exercise for the reader.

Significance: Missing Features

Language features make developers' lives easier. Often, language features are not complex for the language designers to implement (barring unnecessary complications in the internals), but can save developers hours of time. Missing language features are disrespectful to developers and encourage dirty hacks, "clever" solutions, and kludgy workarounds to achieve the desired functionality.

Significance: Implications for Internals

The mere presence of this issue tends to imply some fatal flaw or unnecessary complexity at the most basic levels of the language. For example, an overly complex parser might be trying to compensate for missing functionality in the interpreter by incorrectly (and misleadingly) validating code at the syntax level, or messages without details could indicate that the internal design prohibits access to values where they should be reachable in a sane implementation.