php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #55696 __CLASS__ includes the namespace definition
Submitted: 2011-09-14 20:48 UTC Modified: 2011-09-15 15:30 UTC
From: dohpaz dot php at dohpaz dot com Assigned: nikic (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.3.8 OS:
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
35 - 18 = ?
Subscribe to this entry?

 
 [2011-09-14 20:48 UTC] 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).

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-09-15 15:15 UTC] 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-15 15:24 UTC] nikic@php.net
Automatic comment from SVN on behalf of nikic
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=316819
Log: Make value of __CLASS__ more clear. Refs Bug #55696
 [2011-09-15 15:26 UTC] nikic@php.net
-Status: Open +Status: Closed -Type: Bug +Type: Documentation Problem -Package: Unknown/Other Function +Package: Documentation problem -Assigned To: +Assigned To: nikic
 [2011-09-15 15:26 UTC] 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:28 UTC] 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:30 UTC] dohpaz dot php at dohpaz dot com
nikic,

I can live with this decision. Thank you for the quick response!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC