|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-03-29 17:32 UTC] webseiten dot designer at googlemail dot com
Description:
------------
Please add a way to get the fully qualified class or interface name without the need to create an object. While it is possible to declare a static variable in classes as a workaround, this will not work with interfaces, as you can't declare members inside them. A common trick I've seen is to use a constant:
namespace Foo\Bar;
interface A {
const __NAME = __CLASS__;
}
factory(Bar\A::__NAME); // calls factory with '\Foo\Bar\A'
Though this won't work with inheritance. The magic __NAMESPACE__ constant is also no help when importing namespaces. I propose to use the __CLASS__ keyword to implement such a feature, as it is not allowed to have a class constant of this name:
namespace Foo\Bar {
interface A {}
}
namespace Orange\Banana {
use Foo\Bar;
echo Bar\A::__CLASS__; // should print "Foo\Bar\A"
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 02:00:01 2025 UTC |
I find this idea very good in cases when you want to find all usages of the class over all project inside your IDE. When using class name as hardcoded string this isn't possible. Also in PHP 5.3 possible workaround for this missing feature is to create method "static function __CLASS__() {return get_called_class();}" in all base classes which is extended by other classes. Sure it doesn't help for the interfaces.