Proposal for "nameof" functionality without new keyword...
| From: | Stan Vassilev | FM | Date: | Fri, 21 Nov 2008 15:18:42 +0000 |
| Subject: | Proposal for "nameof" functionality without new keyword... | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-42009@lists.php.net to get a copy of this message | ||
Hi,
Earlier I proposed this construct which fell on deaf ears, maybe since it means a new keyword
"nameof":
namespace Foo\Bar;
class Baz {}
$name = nameof Baz; // 'Foo\Bar\Baz'
The same expansion to FQN happens for imports/aliases with "use XXX".
I want to suggest an alternative approach which could work just the same internally, but without a
new keyword:
namespace Foo\Bar;
class Baz {}
$name = Baz::__NAME__; // 'Foo\Bar\Baz'
[IMPORTANT] Baz::__NAME__ should be replaced at parse time (like the magic constants), so that class
Baz is not autoloaded (we don't need it autoloaded just for the name).
Why is this good:
1) Would cover our basic needs for this feature when passing class names around for factories,
callbacks, strategies, handlers and so on.
2) Doesn't introduce a new keyword, and apparently class constant __NAME__ is very rarely used
by anyone, if at all (Google Code Search shows 0 hits on PHP code).
3) It's intuitive as it follows the conventions established by the other magic constants, and
serves a very similar purpose.
Regards,
Stan Vassilev