Bug #55696 [Com]: __CLASS__ includes the namespace definition
| From: | nikic@php.net | Date: | Thu, 15 Sep 2011 15:15:16 +0000 |
| Subject: | Bug #55696 [Com]: __CLASS__ includes the namespace definition | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-163568@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55696&edit=1
ID: 55696
Comment by: nikic@php.net
Reported by: dohpaz dot php at dohpaz dot com
Summary: __CLASS__ includes the namespace definition
Status: Open
Type: Bug
Package: Unknown/Other Function
PHP Version: 5.3.8
Block user comment: N
Private report: N
New Comment:
As I see it "Foo\Bar" is the expected result. __CLASS__ returns the class name. And the
class name is "Foo\Bar", not "Bar".
An easy way to see this, is writing the following:
$class = __CLASS__;
$obj = new $class;
This typical example (which would obviously be better written as just "$obj = new self;")
would break if only "Bar" would be returned.
Previous Comments:
------------------------------------------------------------------------
[2011-09-14 20:48:27] dohpaz dot php at dohpaz dot com
Description:
------------
With the introduction of namespaces, the __CLASS__ magic constant has changed (without being
documented) to include the current namespace as part of the class name. I submit that since there is
a __NAMESPACE__ magic constant that the __CLASS__ constant should be reverted to its previous
behavior. It seems more natural to concatenate __NAMESPACE__ and __CLASS__ to get a qualified name,
rather than using basename() to get just the class name.
At the very least, the documentation for namespaces (http://php.net/namespace), Magic Constants
(http://us.php.net/manual/en/language.constants.predefined.php), and Backwards Incompatible Changes
(http://us.php.net/manual/en/migration53.incompatible.php) should be updated to reflect this change.
Test script:
---------------
<?php
namespace Foo;
class Bar {
public function __construct() {
echo __CLASS__ . PHP_EOL;
}
}
$bar = new Bar; // echo's Foo\Bar
?>
Expected result:
----------------
I expect the above test script to return just the class name (i.e., Bar).
Actual result:
--------------
The test script above returns the qualified class name (i.e., Foo\Bar).
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55696&edit=1