Re: Enhanced __CLASS__ constant

From: Date: Thu, 22 Jan 2009 07:24:49 +0000
Subject: Re: Enhanced __CLASS__ constant
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-42766@lists.php.net to get a copy of this message
Especially in namespaced code it should be very useful to have "something" returning fully qualified name as string. It can be operator, but I think introducing new keyword is bad way, or it can be realized using well known magic constant in new context: Example: function factory($class) { return new $class; } $obj = factory('MyClass'); // returns object MyClass $obj = factory(MyClass::__CLASS__); // returns again object MyClass, code is little bit more selfexplaining
Hi, I've discovered this omission earlier and I have proposed two solutions: 1) $x = nameof Foo; -or- 2) $x = Foo::__NAME__; // very similar to yours, Foo::__CLASS__ may even be better Since passing identifier references as strings is relatively pervasive in a lot of the PHP code out there, I hope this is resolved for 5.3, before we figure out we need it the hard way after we've littered our code with tons of manually written out explicit references. One problem with your proposal / my second proposal is, it works for classes, but doesn't address namespaced 1) functions 2) constants. A new keyword would address this, but then, who wants a new keyword, right... Regards, Stan Vassilev

« previous php.internals (#42766) next »