# Proposal for new construct: nameof

- Thread: https://externals.io/message/41544
- Original archive: https://news-web.php.net/php.internals/41544
- Started by Stan Vassilev | FM on 2008-10-29T07:35:22+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #41544 — Stan Vassilev | FM — 2008-10-29T07:35:22+00:00

URL: https://externals.io/message/41544#41544

Hi,

With namespaces and "use" we'll be introducing a new kind of discrepancy between a string reference to a function, and the short "use"-enhanced name for the same function. This becomes very painful, when I want to load a function before I run it.

Today I would do (due to lack of function autoload.. hopefully one day..):   

F::loadFunc('Foo_Bar_Baz');  
echo Foo_Bar_Baz();

Tommorow I could attempt to do:

use Foo\Bar as B;
F::loadFunc('B\Baz'); <-- failure, strings are not resolved agains the use statements
echo B\Baz(); 

so it becomes:

use Foo\Bar as B;
F::loadFunc('Foo\Bar\Baz'); <-- works, but negates some of the use for "use"...
echo B\Baz(); 

I suggest we introduce a new construct which will return a string when passed any identifier to resolve against the current file's use clauses:

nameof Symbol\Name;

With this identifier, the above example can be "normalized" to the following code:

use Foo\Bar as B;
F::loadFunc(nameof B\Baz); <-- will send 'Foo\Bar\Baz' as the argument
echo B\Baz(); 

NOTE: As a side effect this means less direct writing of string function/class names, and less complaints about \ being the escape characters.

Regards, 
Stan Vassilev

---

## #41563 — Edward Z. Yang — 2008-10-29T19:50:59+00:00

URL: https://externals.io/message/41544#41563 · In reply to: #41544

Stan Vassilev | FM wrote:
> I suggest we introduce a new construct which will return a string when passed any identifier to resolve against the current file's use clauses:
> 
> nameof Symbol\Name;
> 
> With this identifier, the above example can be "normalized" to the following code:

I second this suggestion. One thing that would be awesome is if we could
drop nameof completely (i.e. all class/function names are first class
data) like F::loadFunc(B\Baz), but I doubt it would be possible with the
parser (at the very least, it wouldn't work for items without a
namespace separator, since it could be construed as a constant).

Cheers,
Edward

---

## #41573 — Karsten Dambekalns — 2008-10-30T12:30:53+00:00

URL: https://externals.io/message/41544#41573 · In reply to: #41544

Hi.

Stan Vassilev | FM wrote:
> I suggest we introduce a new construct which will return a string
> when passed any identifier to resolve against the current file's use
> clauses:
> 
> nameof Symbol\Name;
...
> NOTE: As a side effect this means less direct writing of string
> function/class names, and less complaints about \ being the escape
> characters.

Oh, that sounds very good. We do a lot of getComponent($classname) 
stuff, that would be awesome in that context.

Regards,
Karsten
