Bug #55214 [Opn->Asn]: use of __CLASS__ within trait returns trait name not class name
| From: | gron@php.net | Date: | Sat, 23 Jul 2011 12:17:20 +0000 |
| Subject: | Bug #55214 [Opn->Asn]: use of __CLASS__ within trait returns trait name not class name | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-162128@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55214&edit=1
ID: 55214
Updated by: gron@php.net
Reported by: chris dot rutledge at gmail dot com
Summary: use of __CLASS__ within trait returns trait name not
class name
-Status: Open
+Status: Assigned
Type: Bug
-Package: Documentation problem
+Package: Scripting Engine problem
Operating System: Ubuntu
PHP Version: 5.4.0alpha1
-Assigned To:
+Assigned To: gron
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2011-07-22 04:56:04] gron@php.net
Felipe: I tend to disagree, too.
I do not think this is expected behavior.
Will have a look at this, and another bug reported on the QA mailing list hopefully at the weekend.
Best regards
Stefan
------------------------------------------------------------------------
[2011-07-15 06:44:47] chris dot rutledge at gmail dot com
It may be expected from the point of view of those developing the 5.4 branch, but
logically this approach seems flawed.
As one big advantage of traits seems to be as a replacement for copy-and-pasting
code, maybe consider making __CLASS__ have the value of the class that called the
trait and a new magic constant __TRAIT__ with the trait name?
------------------------------------------------------------------------
[2011-07-15 06:32:57] felipe@php.net
This is the expected behavior. It just need be documented.
------------------------------------------------------------------------
[2011-07-15 06:30:34] chris dot rutledge at gmail dot com
Description:
------------
use of __CLASS__ within trait returns trait name not class name.
Test script:
---------------
trait Singleton {
private static $instance;
public static function Load() {
if(!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
}
class Test {
use Singleton;
private function __construct() { }
}
Test::Load();
Expected result:
----------------
Expected __CLASS__ to return the name of the class that required the trait ('Test'), so
that the singleton object could be instantiated
Actual result:
--------------
__CLASS__ returned 'Singleton', this caused a Fatel error:
Fatal error: Cannot instantiate trait Singleton in /home/dshed/public/northie/index.php on line 7
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55214&edit=1