Req #54417 [Opn->Csd]: Add string casting support for class/interface names
| From: | ajf@php.net | Date: | Sun, 04 Jan 2015 19:26:11 +0000 |
| Subject: | Req #54417 [Opn->Csd]: Add string casting support for class/interface names | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189647@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=54417&edit=1
ID: 54417
Updated by: ajf@php.net
Reported by: webseiten dot designer at googlemail dot com
Summary: Add string casting support for class/interface names
-Status: Open
+Status: Closed
Type: Feature/Change Request
Package: Class/Object related
Operating System: N/A
PHP Version: Irrelevant
-Assigned To:
+Assigned To: ajf
Block user comment: N
Private report: N
New Comment:
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at
http://www.php.net/downloads.php
PHP 5.5 added this (see RFC: https://wiki.php.net/rfc/class_name_scalars)
You can do ClassName::class to get its fully-qualified name.
Previous Comments:
------------------------------------------------------------------------
[2011-09-21 11:42:20] aigors at inbox dot lv
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.
------------------------------------------------------------------------
[2011-03-29 17:32:08] 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"
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=54417&edit=1