Robert Landers has submitted the nameof() RFC to the PHP internals list for discussion. The proposal introduces a compiler-level construct that looks like a function but behaves like ::class: it resolves to a string at compile time and can be used anywhere a string literal is allowed, including constant definitions, array keys and attribute arguments.
The construct covers variables, properties, methods, functions and constants. For example, nameof($this->myProperty) would evaluate to "myProperty", and nameof($a->exampleB(...)) to "exampleB", using first-class callable syntax for functions and methods.
A central point of debate is name resolution. In the current draft, constants and functions return the lexical name rather than the fully qualified one: if a function is imported with use function Acme\bar as foo, nameof(foo(...)) yields "foo". Writing the fully qualified name inside nameof() returns the full name. Landers defends this with trait aliases and error messages, where the local name is often the more useful one. Rowan Tommins argues the opposite: the main value over plain strings would be namespace expansion, as ::class does, especially when the string is passed to code in another file, such as an attribute constructor.
Error handling is still open. Landers outlined two options: no checking at all, treating nameof() as pure string manipulation like ::class, or emitting a warning for names that do not exist, which could be silenced with @. Tommins raised the question of when resolution would happen, since compile-time checks cannot see symbols in other files and runtime checks raise autoloading questions. Landers said he updated the RFC with details after feedback from Dan Ackroyd.
Lydia de Jongh suggested a magic-constant style syntax such as MyClass::$property::name instead of a function call, arguing it would work better in attributes and editors. Tommins noted that ::class is not actually a constant and nameof() not actually a function; both are compiler syntaxes producing strings, so the function-like form works in attributes too. Landers also pointed out that a ::name constant could collide with existing user constants named "name".
Dan Ackroyd criticised the plan to leave design choices to a secondary vote. He said the RFC process exists to force authors to think through details themselves, and offering options for a vote lets them skip that work. Landers said he would add concrete usage examples and error condition details to the RFC. The discussion remains in the discussion phase; no vote has been called.




Comments
No comments yet — be the first.
Open the discussion
No account or password needed — just enter your e-mail and we’ll send you a one-time sign-in link. First time here? You’re set up automatically.
Your rating will be applied automatically after you sign in.
Check your inbox
We’ve sent a sign-in link to …. Open it on this device — this tab will sign you in automatically.
Nothing arrived? Check your spam folder — and mark the mail as "Not spam" so it lands in your inbox next time.