knave Posted April 10, 2008 Share Posted April 10, 2008 From a static method, is it possible to definitively get the name of the class it is *running* in, even if the method was inherited? I know the self keyword and __CLASS__ constant are both bound at compile-time to the class the method was *defined* in, which is not what I want. As well, calling the built-in get_class() without an argument also uses the compile-time bound class. Take the following code example: class parent_class { public function foo() { // do stuff... // attempting to call the bar method of the run-time bound class... call_user_func( array( get_class(), 'bar' ) ); } // this is the compile-time bound class protected static function bar() { // do something... echo "parent!"; } } class child_class extends parent_class { // this is the run-time bound class protected static function bar() { // do something ELSE... echo "child!"; } } // invoke parent_class::bar() parent_class::foo(); // ideally, invoke child_class::bar() child_class::foo(); I can't simply bypass foo, because it does a significant amount of stuff *before* calling bar. Is there anything I can do without changing the entire structure of the class(es) from static to instantiated? Quote Link to comment https://forums.phpfreaks.com/topic/100531-get-effective-class-name-from-a-static-method/ Share on other sites More sharing options...
keeB Posted April 10, 2008 Share Posted April 10, 2008 You've given me a headache. Whenever people have questions like this (and yes, it happens often enough) it usually results in a fatal flaw in design and indicative of poor forethought. Why can't you do the following? <?php class parent_class { public function foo() { // do stuff... // attempting to call the bar method of the run-time bound class... call_user_func( array( get_class(), 'bar' ) ); } // this is the compile-time bound class protected static function bar() { // do something... echo "parent!"; } } class child_class extends parent_class { // this is the run-time bound class protected static function bar() { // do something ELSE... self::foo(); // added. echo "child!"; } } // invoke parent_class::bar() parent_class::foo(); // ideally, invoke child_class::bar() child_class::foo(); ?> Or am I missing the point completely? Quote Link to comment https://forums.phpfreaks.com/topic/100531-get-effective-class-name-from-a-static-method/#findComment-514205 Share on other sites More sharing options...
knave Posted April 10, 2008 Author Share Posted April 10, 2008 First, I should note that I mistyped in the original code example...The method parent_class::foo should be static. public static function foo() { // do stuff... // attempting to call the bar method of the run-time bound class... call_user_func( array( get_class(), 'bar' ) ); } As for your suggestion, keeB, I don't really understand what you mean. The entry point into the class is the public static method foo, which does some preprocessing, then calls the protected static method bar. What I want to do, is call the child_class::bar method within foo instead of parent_class::bar. Your change is in the child bar method that never gets called. I may be completely mis-communicating what I want to do, and if so I apologize. Quote Link to comment https://forums.phpfreaks.com/topic/100531-get-effective-class-name-from-a-static-method/#findComment-514252 Share on other sites More sharing options...
Daniel0 Posted April 11, 2008 Share Posted April 11, 2008 You cannot do that. Late static binding is not supported yet. It's coming in PHP 5.3.0. http://php.net/manual/en/language.oop5.late-static-bindings.php Edit: It seems there is a comment on the bottom suggesting that you can use get_class() to get around that. You might want to check that out. Quote Link to comment https://forums.phpfreaks.com/topic/100531-get-effective-class-name-from-a-static-method/#findComment-514419 Share on other sites More sharing options...
keeB Posted April 11, 2008 Share Posted April 11, 2008 Can you give me an example where something like this would be useful? Apparently I'm an idiot because I can't think of a single reason why. Quote Link to comment https://forums.phpfreaks.com/topic/100531-get-effective-class-name-from-a-static-method/#findComment-514430 Share on other sites More sharing options...
aschk Posted April 11, 2008 Share Posted April 11, 2008 Does anyone else think it might be possible using Reflection, as a workaround? I haven't tried but it was the 1st thing that came to mind when I saw the problem question. Quote Link to comment https://forums.phpfreaks.com/topic/100531-get-effective-class-name-from-a-static-method/#findComment-514546 Share on other sites More sharing options...
knave Posted April 11, 2008 Author Share Posted April 11, 2008 My particular use is admittedly an edge case, where I'm extending an entirely static class (all static methods and properties). In truth, I'm using it more as a workaround for the lack of namespaces. The workaround using get_class only works if you have an instance of the object, which I don't in this case because I'm using all static functions. So, as that example suggests, I'm going to turn what was a completely static class into a sort of singleton, where I'll have an instance that I can use to get the run-time class. As far as using Reflection, I looked into that as well. It faces the same problem as get_class, where you need to specify an instance. Thanks, all. Quote Link to comment https://forums.phpfreaks.com/topic/100531-get-effective-class-name-from-a-static-method/#findComment-514821 Share on other sites More sharing options...
Jenk Posted April 16, 2008 Share Posted April 16, 2008 static methods are not bound by inheritance. What you are doing is calling a method as if it were a function, i.e. not in a class at all. A class to a static method is nothing more than a namespace. Quote Link to comment https://forums.phpfreaks.com/topic/100531-get-effective-class-name-from-a-static-method/#findComment-518358 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.