Edit report at https://bugs.php.net/bug.php?id=55214&edit=1
ID: 55214
Updated by: felipe@php.net
Reported by: chris dot rutledge at gmail dot com
Summary: use of __CLASS__ within trait returns trait name not
class name
Status: Assigned
Type: Bug
Package: Scripting Engine problem
Operating System: Ubuntu
PHP Version: 5.4.0alpha1
Assigned To: gron
Block user comment: N
Private report: N
New Comment:
It's simple to add the __TRAIT__ one, just like __CLASS__ does.
But making a more magic __CLASS__ to reflect the class that called is too much magic, hacky. Whereas
we are currently doing the __CLASS__ substitution on compile-time.
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