Bug #55214 [Com]: use of __CLASS__ within trait returns trait name not class name

From: Date: Fri, 15 Jul 2011 10:44:47 +0000
Subject: Bug #55214 [Com]: use of __CLASS__ within trait returns trait name not class name
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-6884@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 Comment by: chris dot rutledge at gmail dot com Reported by: chris dot rutledge at gmail dot com Summary: use of __CLASS__ within trait returns trait name not class name Status: Open Type: Bug Package: Documentation problem Operating System: Ubuntu PHP Version: 5.4.0alpha1 Block user comment: N Private report: N New Comment: 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? Previous Comments: ------------------------------------------------------------------------ [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

« previous php.doc.bugs (#6884) next »