# Pre-RFC discussion: nameof

- Thread: https://externals.io/message/120173
- Original archive: https://news-web.php.net/php.internals/120173
- Started by Robert Landers on 2023-05-01T19:00:04+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #120173 — Robert Landers — 2023-05-01T19:00:04+00:00

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

Hello internals,

After seeing some discussions about using enum values as keys, I
thought of my own implementation of `nameof` (from C#) that is very
hackish: https://github.com/withinboredom/nameof

I'd like to propose making it a real thing (with better behavior than
my hack). An operator like this has a few niche uses, mostly for
better error messages but also for making strings of things that you
normally can't get the string value of. Here are some examples:

nameof(func(...)) === 'func'
nameof($party) === 'party'
nameof($captain->planet) === 'planet'
nameof(Class::Prop) === 'Prop'

I first attempted this as an extension, but this belongs at the
parsing/compiling level and not during runtime (this gets turned into
a constant zval).

I use my (very hacky) implementation to handle stringifying enums
(mostly), as well as better, easier-to-refactor error messages:

throw new InvalidArgumentException(nameof($var) . ' has an unexpected value');

Without it, the name of the variable can be missed in a refactor,
making the error message useless or incorrect.

The code to make this possible (without much error checking) is
relatively simple: https://github.com/php/php-src/pull/11172

Robert Landers
Software Engineer
Utrecht NL
