> I totally agree with Rowan. In many (most?) cases you need the fully
> qualified name.
> Even or especially for error handling! Of course mostly the error is
> somewhere else. But you want to know the starting point. An
> unqualified name gets you nowhere. Although backtrace can help you
> out.
>
> And this is also consistent with how  ::class or get_class() work....
> it gives fully qualified names.

> On the other hand you could argue that 'nameof()' in itself means:
> 'just looking for the direct/unqualified name of something' and is an
> addition to ::class and get_class()....

This is exactly what I was thinking when I wrote it out, however, you
can pass the full name to get the full name (an escape hatch of
sorts).

> Then we still need something to get the fully qualified name/path of a
> variable or php-item.
> For a class we have: ::class, get_class() and class_exists().
> And inside these function you can use:
> get_class(MyClass::class);

Variables don't get stored in a namespace or they exist only in the
local scope (they can't have a 'qualified' name, in my experience):

namespace A { $b = 'c'; }
echo $b; // outputs: 'c'

For callables, you can already get the fully qualified name:

echo (new ReflectionFunction(strstr(...)))->getName();

You, however, cannot get the name of a constant, except through
get_defined_constants() and comparing values (searching in the
namespace you care about) and doesn't help you if it isn't defined.

There's further evidence that an unqualified name is the right way to go:

use const \NoExist\TEST;
use \NoExist\Channel;
echo TEST;
echo Channel::class;

the output is:

Undefined constant "TEST" in ...
Channel

note that it does NOT say "\MyNamespace\TEST" is undefined nor does it
output "\NoExist\Channel", so if the user wants to write:

use function \AppPlugins\Config;

if(!function_exists(nameof(Config(...))))
 throw new Exception(nameof(Config(...)) . ' does not exist, please
define it.');

It's going to output the unqualified name anyway, simply due to how
PHP resolves names. Using the "escape hatch", however, we can "force"
PHP to do what we expect:

if(!function_exists(nameof(\AppPlugins\Config(...)))
  throw new Exception(nameof(Config(...)) . ' does not exist, please
define it.');

I'll update the RFC to reflect this.

> The downside is that we get many new 'magic constants'...

There are no magic constants in this RFC.

Robert Landers
Software Engineer
Utrecht NL

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

Reply via email to