Welcome! Log In Create A New Profile


[PHP-DEV] Reflection API: Add ->getTerminatedNamespace() method

Posted by Andreas Hennings 
Andreas Hennings
[PHP-DEV] Reflection API: Add ->getTerminatedNamespace() method
December 11, 2017 02:00AM
## Background / motivation

Currently a number of reflection classes have a method ->getNamespaceName().

The namespace comes in a format that is not straightforward to work with.
It requires the root namespace to be treated in a special way.

$namespace = $relfClass->getNamespaceName();
$shortname = $reflClass->getShortName();
$class = $reflClass->getName();

if ('' === $namespace) {
assert($class === $namespace . $shortname);
assert($class === $shortname);
assert($class !== $namespace . '\\' . $shortname);
else {
assert($class === $namespace . '\\' . $shortname;
assert($class !== $namespace . $shortname);

Code that deals with namespaces therefore always needs a special case
for the root namespace.

## Proposal

I propose that reflection components that currently have a
->getNamespaceName() method get another method
This comes with ending namespace separator, unless it is the root namespace.

namespace Acme\Animal;
class Cat {}

assert('Acme\Animal\\' === (new
assert('Acme\Animal' === (new

assert('' === (new
assert('' === (new \ReflectionClass(\stdClass::class))->getNamespaceName());

foreach ([Cat::class, \stdClass::class] as $class) {
$reflClass = new \ReflectionClass($class);
assert($reflClass->getName() ===
$reflClass->getTerminatedNamespace() . $reflClass->getShortName());

This would allow all cases to be covered by the same logic.

PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login