ID: 34804 Comment by: benjaminhill at gmail dot com Reported By: toomuchphp-phpbugs at yahoo dot com Status: Open Bug Type: Feature/Change Request Operating System: Any PHP Version: 5.x New Comment:
even something as simple as... class Parent { static function myCurrentClassName() { return (__CALLINGCLASS__); } } class Child extends Parent { // empty } echo Child::myCurrentClassName() returns "Child" Previous Comments: ------------------------------------------------------------------------ [2006-06-24 23:20:38] DouglasStewart at creighton dot edu Understood toomuch, thanks. I would then request the following: Please provide the following on the static side: $this -- in the context of a class side(static) method. $this represents an instance of ReflectionClass with all methods answering correctly as to the context of the call. So, for instance, statically implemented "$this->fileName()" would return the fileName that the current context class was loaded from. With this feature in place, I am confident that self will no longer be used for anything except in a root class like Object. $sender -- Either a ReflectionClass instance when the call is made from a class-side implementation or a regular object instance. class() -- An instance method that all objects inherit, that returns an instance of ReflectionClass for the class of the object context making the call. Smalltalk offers some rather elegant solutions to these true OO problems that PHP's sudo-OO implementation struggles with. ------------------------------------------------------------------------ [2006-06-19 23:44:53] toomuchphp-phpbugs at yahoo dot com The reason why some of the bug reports get dismissed so easily is because they ask for one of the existing features ('self' or '__CLASS__') to be modified to solve this problem, but 'self' and '__CLASS__' are both already very useful and proably very widely used, so it's not good to change them, and therefore we need a *new way* to find out what class the static method was part of. Something I find very disappointing is the fact that bug #30828 destroyed the only existing solution to this problem - debug_backtrace(). In PHP 5.0.4, debug_backtrace() could have been used here to discover the static class name, but somebody wanted it working the other way (like __CLASS__ instead), the bug was reported, fixed in 5.0.5, and so we lost our only solution to this problem. This bugfix actually broke some of my existing code a year ago when it was released, but it wasn't so important to me at the time. It's only over the past year as I've been writing object-oriented code full time, and studying implementations by other languages (Java, AspectJ, Ruby) that I've realized how damaging the change to debug_backtrace() was. I am hoping that as Zend continue writing their Framework, they'll realize just how broken static methods are and will try to fix them somehow. ------------------------------------------------------------------------ [2006-06-19 20:41:34] DouglasStewart at creighton dot edu 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. ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/34804 -- Edit this bug report at http://bugs.php.net/?id=34804&edit=1