Doc #55696 [Csd]: __CLASS__ includes the namespace definition
| From: | dohpaz dot php at dohpaz dot com | Date: | Thu, 15 Sep 2011 15:30:16 +0000 |
| Subject: | Doc #55696 [Csd]: __CLASS__ includes the namespace definition | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-7196@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
User updated by: dohpaz dot php at dohpaz dot com
Reported by: dohpaz dot php at dohpaz dot com
Summary: __CLASS__ includes the namespace definition
Status: Closed
Type: Documentation Problem
Package: Documentation problem
PHP Version: 5.3.8
Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
nikic,
I can live with this decision. Thank you for the quick response!
Previous Comments:
------------------------------------------------------------------------
[2011-09-15 15:28:53] nikic@php.net
Sorry for triple-post. The previous comment contains the wrong snippet (Buggy Bugtracker ^^). Should
say:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation better.
------------------------------------------------------------------------
[2011-09-15 15:26:54] nikic@php.net
This bug has been fixed in SVN.
Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
For Windows:
http://windows.php.net/snapshots/
Thank you for the report, and for helping us make PHP better.
===
I updated the predefined constants documentation to make this more clear :)
------------------------------------------------------------------------
[2011-09-15 15:24:34] nikic@php.net
Automatic comment from SVN on behalf of nikic
Revision: http://svn.php.net/viewvc/?view=revision&revision=316819
Log: Make value of __CLASS__ more clear. Refs Bug #55696
------------------------------------------------------------------------
[2011-09-15 15:15:16] nikic@php.net
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.
------------------------------------------------------------------------
[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