#34804 [Com]: More information about stclass methods

From: Date: Mon, 19 Jun 2006 20:41:36 +0000
Subject: #34804 [Com]: More information about stclass methods
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-98520@lists.php.net to get a copy of this message
ID: 34804 Comment by: DouglasStewart at creighton dot edu Reported By: toomuchphp-phpbugs at yahoo dot com Status: Open Bug Type: Feature/Change Request Operating System: Any PHP Version: 5.x New Comment: I am unsure where to comment on this. This particular issue that folks are having with __CLASS__ and self not playing in the proper calling context makes life very difficult. The bug report responses like "This is not a bug" and "is designed and intended to work that way" are, I suppose, the discretion of the current developer base or ZEND or whoever. Maybe if someone could elaborate on the wisdom of this design decision. Imparting this knowledge may illuminate the reasons why this feature that many OO programmers are accustomed to having available is being dismissed so readily. The current implementation of __CLASS__ is not useful. If you wish to ask a class for its name, php is not yet prepared to provide you with the answer. The following examples illustrate why: class TopClass { public static function ClassNameIndirect() { return self::ClassName(); } public static function ClassName() { return 'TopClass'; } public static function ClassNameKeyword() { return __CLASS__; } public static function ClassNameKeywordUninherited() { return self::ClassNameKeyword(); } public static function ClassNameSelfInstance() { $object = new self; return get_class($object); } public static function ClassNameSelfInstanceUninherited() { return self::ClassNameSelfInstance(); } public static function ClassNameKeywordUninheritedIndirect() { return self::ClassNameKeywordUninherited(); } public static function ClassNameSelfInstanceUninheritedIndirect() { return self::ClassNameSelfInstanceUninherited(); } public static function ClassNameHack() { $bt = debug_backtrace(); $name = $bt[0]['class']; return $name; } public static function ClassNameHackUninherited() { return self::ClassNameHack(); } } class BottomClass extends TopClass { public static function ClassName() { return 'BottomClass'; } public static function ClassNameKeywordUninherited() { return __CLASS__; } public static function ClassNameSelfInstanceUninherited() { $object = new self; return get_class($object); } public static function ClassNameHackUninherited() { $bt = debug_backtrace(); $name = $bt[0]['class']; return $name; } } echo '<br />(1)*** Results for a static and constant class name implementation ***'; echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameIndirect(); echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameIndirect(); echo '<br />'; echo '<br />(2)*** Results for an inherited __CLASS__ implementation ***'; echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameKeyword(); echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameKeyword(); echo '<br />'; echo '<br />(3)*** Results for an UNinherited __CLASS__ implementation ***'; echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameKeywordUninherited(); echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameKeywordUninherited(); echo '<br />'; echo '<br />(4)*** Results for an Instance generated class name ***'; echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameSelfInstance(); echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameSelfInstance(); echo '<br />'; echo '<br />(5)*** Results for an UNinherited Instance generated class name ***'; echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameSelfInstanceUninherited(); echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameSelfInstanceUninherited(); echo '<br />'; echo '<br />(6)*** Results for an inherited hack to get class name ***'; echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameHack(); echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameHack(); echo '<br />'; echo '<br />(7)*** Results for an UNinherited hack to get class name ***'; echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameHackUninherited(); echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameHackUninherited(); echo '<br />'; echo '<br /> ---'; echo '<br />'; echo '<br />(8)*** #3 above with inherited method that calls the UNinherited __CLASS__ implementation ***'; echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameKeywordUninheritedIndirect(); echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameKeywordUninheritedIndirect(); echo '<br />'; echo '<br />(9)*** #5 above with inherited method that calls the UNinherited Instance generated class name ***'; echo '<br />ClassName() for TopClass: ' . TopClass::ClassNameSelfInstanceUninheritedIndirect(); echo '<br />ClassName() for BottomClass: ' . BottomClass::ClassNameSelfInstanceUninheritedIndirect(); // ** // ** Results // ** (1)*** Results for a static and constant class name implementation *** ClassName() for TopClass: TopClass ClassName() for BottomClass: TopClass (2)*** Results for an inherited __CLASS__ implementation *** ClassName() for TopClass: TopClass ClassName() for BottomClass: TopClass (3)*** Results for an UNinherited __CLASS__ implementation *** ClassName() for TopClass: TopClass ClassName() for BottomClass: BottomClass (4)*** Results for an Instance generated class name *** ClassName() for TopClass: TopClass ClassName() for BottomClass: TopClass (5)*** Results for an UNinherited Instance generated class name *** ClassName() for TopClass: TopClass ClassName() for BottomClass: BottomClass (6)*** Results for an inherited hack to get class name *** ClassName() for TopClass: TopClass ClassName() for BottomClass: TopClass (7)*** Results for an UNinherited hack to get class name *** ClassName() for TopClass: TopClass ClassName() for BottomClass: BottomClass --- (8)*** #3 above with inherited method that calls the UNinherited __CLASS__ implementation *** ClassName() for TopClass: TopClass ClassName() for BottomClass: TopClass (9)*** #5 above with inherited method that calls the UNinherited Instance generated class name *** ClassName() for TopClass: TopClass ClassName() for BottomClass: TopClass -- Examples #3 & #5 appear, at first, to work correctly, but after further testing (see #8 & #9), it becomes clear that the answer to the request for class name on the static (class) side of things is an exercise in futility. Previous Comments: ------------------------------------------------------------------------ [2006-06-01 02:33:17] toomuchphp-phpbugs at yahoo dot com So ... now that I have learned C and played around with the 5.1.4 source for a couple of days, I've discovered it's not really possible to get information about the calling context using matic constants. In which case, a function named something like 'get_static_child()' would suffice. I am still trying to figure out how to find the name of the child class in the Zend hive of structs (if it is actually stored somewhere). :-S ------------------------------------------------------------------------ [2006-01-11 21:35:07] kvesteri at cc dot hut dot fi There should definetly be atleast constant __INHERITED_BY__. This new constant would allow many wonderful things like a generic singleton class and an efficient implementation of the martin fowler's Active Record pattern. ------------------------------------------------------------------------ [2005-10-10 10:12:46] toomuchphp-phpbugs at yahoo dot com Description: ------------ Inside any class method, there are as many as 3 related class and it would be useful to have all of them as magic constants, and also in the debug_backtrace function. Currently only __CLASS__ is available (the name of the direct owner of the method). It would be good to know the name of the child class (if the method was called as part of a child class), and the name of the calling class if the method was called statically (the same as get_class($this) inside a static method). This information would be most useful in the debug_backtrace() array. debug_backtrace() was recently modified to report the direct owner class name rather than the inheriting class' name (see bug #30828) but it would really be more helpful in debugging to have all three possible class names available. Reproduce code: --------------- class A { function A() { echo 'direct owner: '.__CLASS__."\n"; echo 'called as part of: '.__INHERITED_BY__."\n"; echo 'called by instance of: '.__STATIC_CALLER__."\n"; } } class B extends A { } class C { function __construct() { B::A(); } } new C(); Expected result: ---------------- direct owner: A called as part of: B called by instance of: C ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=34804&edit=1

« previous php.bugs (#98520) next »