|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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). PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 11 01:00:02 2025 UTC |
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.