Re: [PHP] Re: Get name of extending class with static method call
Torsten, I also found the following link to be helpful. Check out the user notes from michael at digitalgnosis dot removethis dot com (he did something similar to what I have already suggested, i.e. call_user_func) http://www.php.net/manual/en/language.oop5.static.php Hi Jason, thanks for the link. It helped me to find out that this does work: class Car { function drive() { echo 'pre';print_r(debug_backtrace()); } } class Porsche extends Car { function drive() { parent::drive(); } } By tunnelling the call through Porsche's own drive() method debug_backtrace() will contain two traces, one of them with the correct class name. I tried using reflection but reflecting car by using __CLASS__ doesn't give any information about the classes that extend it. So this doesn't work either. Rory's proposed way of reading in the file contents and preg_matching the method name works, but it's very, very ugly, indeed! ;) Isn't it somewhat ridiculous that there is no *easy* way in PHP5 to achieve what I consider to be a pretty straightforward requirement?: Get name of the class that invoked a static call of an inherited method I would like to thank all of those that cared about my problem and tried to find a solution by providing a wide variety of ideas. Thank you very much!!! Do you think it would be wise to ask for help on php-dev? Best regards, Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
By tunnelling the call through Porsche's own drive() method debug_backtrace() will contain two traces, one of them with the correct class name. I tried using reflection but reflecting car by using __CLASS__ doesn't give any information about the classes that extend it. So this doesn't work either. Right... the root issue here seems to be that everything is relying on the __CLASS__ macro, but this macro behaves differently than what you expected (a la debug_backtrace in PHP4) Rory's proposed way of reading in the file contents and preg_matching the method name works, but it's very, very ugly, indeed! ;) Isn't it somewhat ridiculous that there is no *easy* way in PHP5 to achieve what I consider to be a pretty straightforward requirement?: Get name of the class that invoked a static call of an inherited method Indeed! I was actually quite surprised that this wasn't the way __CLASS__ resolved... I had to code it to believe it (and I didn't even do that until after you told us __CLASS__ didn't work!) I would like to thank all of those that cared about my problem and tried to find a solution by providing a wide variety of ideas. Thank you very much!!! Do you think it would be wise to ask for help on php-dev? Perhaps the way to go about this would be to make a feature request on bugs.php.net? It does seem like something that should be a part of the language. -- Teach a person to fish... Ask smart questions: http://www.catb.org/~esr/faqs/smart-questions.html PHP Manual: http://www.php.net/manual/en/index.php php-general archives: http://marc.theaimsgroup.com/?l=php-generalw=2 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Jason Barnett wrote: Indeed! I was actually quite surprised that this wasn't the way __CLASS__ resolved... I had to code it to believe it (and I didn't even do that until after you told us __CLASS__ didn't work!) I would like to thank all of those that cared about my problem and tried to find a solution by providing a wide variety of ideas. Thank you very much!!! Do you think it would be wise to ask for help on php-dev? I'll put money on that they say won't do it and then tell you to read the archives of php-internals as to why. I originally hit this problem when I started to write a PHP5 based framework at the end of 2003. during the time since then I have watched/read quite a few threads on this subject. I strongly suggest you brew a big pot of coffee and sit down for a few (lots??) hours to read thru what has been discussed on php-internals, you might start with: Perhaps the way to go about this would be to make a feature request on bugs.php.net? It does seem like something that should be a part of the language. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
darn, send button again!! (sent too early) Jochem Maas wrote: Jason Barnett wrote: Indeed! I was actually quite surprised that this wasn't the way __CLASS__ resolved... I had to code it to believe it (and I didn't even do that until after you told us __CLASS__ didn't work!) I would like to thank all of those that cared about my problem and tried to find a solution by providing a wide variety of ideas. Thank you very much!!! Do you think it would be wise to ask for help on php-dev? I'll put money on that they say won't do it and then tell you to read the archives of php-internals as to why. I originally hit this problem when I started to write a PHP5 based framework at the end of 2003. during the time since then I have watched/read quite a few threads on this subject. my solution used call_user_func_array(), its relatively slow and a ing nightmare in terms of complexity. I strongly suggest you brew a big pot of coffee and sit down for a few (lots??) hours to read thru what has been discussed on php-internals, you might start with: http://www.zend.com/lists/php-dev/200307/msg00242.html basically, AFAIKR, the final answer was something along the lines of - 'your misusing the static class concept' (don't hold me to that tho!) Perhaps the way to go about this would be to make a feature request on bugs.php.net? It does seem like something that should be a part of the language. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Hi, I just tried the example code at http://www.zend.com/lists/php-dev/200307/msg00244.html using PHP 5.0.3 The backtrace doesn't see class b at all, all references to it have vanished into thin air. I spent days trying to solve this on my own until I happened upon this thread - it appears that there is no clean way of retrieving the name of the calling class, in a classmethod defined in the superclass. I do not want to overload the sperclass' method. I do not accept that this is misusing the static concept - without it, PHP 5 seems rather lame. How does one go about making a feature request? There has to be a way to get this implemented into PHP 5... Cheers, -Morten Jochem Maas wrote: darn, send button again!! (sent too early) Jochem Maas wrote: Jason Barnett wrote: Indeed! I was actually quite surprised that this wasn't the way __CLASS__ resolved... I had to code it to believe it (and I didn't even do that until after you told us __CLASS__ didn't work!) I would like to thank all of those that cared about my problem and tried to find a solution by providing a wide variety of ideas. Thank you very much!!! Do you think it would be wise to ask for help on php-dev? I'll put money on that they say won't do it and then tell you to read the archives of php-internals as to why. I originally hit this problem when I started to write a PHP5 based framework at the end of 2003. during the time since then I have watched/read quite a few threads on this subject. my solution used call_user_func_array(), its relatively slow and a ing nightmare in terms of complexity. I strongly suggest you brew a big pot of coffee and sit down for a few (lots??) hours to read thru what has been discussed on php-internals, you might start with: http://www.zend.com/lists/php-dev/200307/msg00242.html basically, AFAIKR, the final answer was something along the lines of - 'your misusing the static class concept' (don't hold me to that tho!) Perhaps the way to go about this would be to make a feature request on bugs.php.net? It does seem like something that should be a part of the language. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Morten Rønseth [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Hi, I just tried the example code at http://www.zend.com/lists/php-dev/200307/msg00244.html using PHP 5.0.3 The backtrace doesn't see class b at all, all references to it have vanished into thin air. I spent days trying to solve this on my own until I happened upon this thread - it appears that there is no clean way of retrieving the name of the calling class, in a classmethod defined in the superclass. I do not want to overload the sperclass' method. I do not accept that this is misusing the static concept - without it, PHP 5 seems rather lame. How does one go about making a feature request? There has to be a way to get this implemented into PHP 5... Cheers, -Morten Hi guys, I guess it's time to risk asking on php-dev ;) I will try my luck. Let's see what the gurus say! Regards, Torsten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Torsten Roehr wrote: Morten Rønseth [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Hi, I just tried the example code at http://www.zend.com/lists/php-dev/200307/msg00244.html using PHP 5.0.3 The backtrace doesn't see class b at all, all references to it have vanished into thin air. I spent days trying to solve this on my own until I happened upon this thread - it appears that there is no clean way of retrieving the name of the calling class, in a classmethod defined in the superclass. I do not want to overload the sperclass' method. I do not accept that this is misusing the static concept - without it, PHP 5 seems rather lame. How does one go about making a feature request? There has to be a way to get this implemented into PHP 5... Cheers, -Morten Hi guys, I guess it's time to risk asking on php-dev ;) I will try my luck. Let's see what the gurus say! Regards, Torsten why do you think they'd respond with something else than ask at php.generals ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Because it's a change, that should be reverted, or documented. didou M. Sokolewicz wrote: Torsten Roehr wrote: Morten Rønseth [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Hi, I just tried the example code at http://www.zend.com/lists/php-dev/200307/msg00244.html using PHP 5.0.3 The backtrace doesn't see class b at all, all references to it have vanished into thin air. I spent days trying to solve this on my own until I happened upon this thread - it appears that there is no clean way of retrieving the name of the calling class, in a classmethod defined in the superclass. I do not want to overload the sperclass' method. I do not accept that this is misusing the static concept - without it, PHP 5 seems rather lame. How does one go about making a feature request? There has to be a way to get this implemented into PHP 5... Cheers, -Morten Hi guys, I guess it's time to risk asking on php-dev ;) I will try my luck. Let's see what the gurus say! Regards, Torsten why do you think they'd respond with something else than ask at php.generals ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Morten Rønseth wrote: Hi, I just tried the example code at http://www.zend.com/lists/php-dev/200307/msg00244.html using PHP 5.0.3 The backtrace doesn't see class b at all, all references to it have vanished into thin air. as a side note - using a function like debug_backtrace() seems to be a mis-use -- the debug_ prefix is a pretty clear indication that this is to retrieve info for helping you figure out what is wrong, and therefore it probably not a good idea to write code that relies on its output in a production env. I spent days trying to solve this on my own until I happened upon this thread - it appears that there is no clean way of retrieving the name of the calling class, in a classmethod defined in the superclass. I do not want to overload the sperclass' method. that probably wouldn't solve the problem unless you duplicate the whole function... what about doing this in the superclass yourFunc( $className = '' ) { if (empty($className)) { $className = __CLASS__; } return parent::yourFunc( $className ); } not perfect, but it would allow you to stack the classes and minimize the code in the overloaded funcs. obviously the original func would use the argument to determine which class it was called by. hopefully you get the idea. I do not accept that this is misusing the static concept - without it, actually I agree - but then I'm no expert :-) PHP 5 seems rather lame. How does one go about making a feature request? calling PHP5 lame is a little harsh - one 'missing' feature is not the end of the world - and there are work around. feature requests are done via the bugs DB: http://bugs.php.net/ take the time to thoroughly investigate the DB for similar requests first though, to avoid duplication. Also you may want to politely inquire at [EMAIL PROTECTED] before you do so - possibly they may have some clarification or could point you to earlier discussions that explain the current developer stance. There has to be a way to get this implemented into PHP 5... well if you can write C then definitely - you write it, and submit a patch - even if its not accepted you can roll your own version. :-) Cheers, -Morten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Mehdi Achour wrote: Because it's a change, that should be reverted, or documented. don't top post - its bad form and many people ignore topposts. Mehdi is right to say that debug_backtrace() has changed - but so has the whole engine - I don't think that function was ever meant to be used in the way Morten was (or was it Torsten?). its clearly for debugging, no? and yeah, everything should be documented - but are you going to do? :-) didou ... why do you think they'd respond with something else than ask at php.generals ? bogus! - this is not a case of someone not knowing/understanding existing functionality. its a case of him missing something which quite a few people think should be available to PHP users, ofcourse the developers are entitled to disagree. actually I read php-dev like a zealot (brilliant way to learn about php) and I see the chap in question has posted his question and already got one answer... which didn't mention php-general at all ;-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Jochem Maas wrote: Morten Rønseth wrote: Hi, I just tried the example code at http://www.zend.com/lists/php-dev/200307/msg00244.html using PHP 5.0.3 The backtrace doesn't see class b at all, all references to it have vanished into thin air. as a side note - using a function like debug_backtrace() seems to be a mis-use -- the debug_ prefix is a pretty clear indication that this is to retrieve info for helping you figure out what is wrong, and therefore it probably not a good idea to write code that relies on its output in a production env. I spent days trying to solve this on my own until I happened upon this thread - it appears that there is no clean way of retrieving the name of the calling class, in a classmethod defined in the superclass. I do not want to overload the sperclass' method. that probably wouldn't solve the problem unless you duplicate the whole function... what about doing this in the superclass yourFunc( $className = '' ) { if (empty($className)) { $className = __CLASS__; } return parent::yourFunc( $className ); } I'll have to wait until I get to work tomorrow to check this one out. But offhand I cannot see it working as I want it to. Sorry. not perfect, but it would allow you to stack the classes and minimize the code in the overloaded funcs. obviously the original func would use the argument to determine which class it was called by. hopefully you get the idea. I do not accept that this is misusing the static concept - without it, actually I agree - but then I'm no expert :-) PHP 5 seems rather lame. How does one go about making a feature request? calling PHP5 lame is a little harsh - one 'missing' feature is not the end of the world - and there are work around. I feel that this 'missing feature' is a sadly lacking implementation of the concept and use of classmethods. The gurus/implementors might argue otherwise, but I believe that being able to tell the class of the originating call is a logical cause of the static (classmethod) concept - without it the implementation of classmethods seems incomplete, and hence lame. I probably should have made this clearer... Also, I believe that any workaround here is highly undersirable. With PHP5 we got a very decent object model which made it possible to model and implement our solutions in a professional way. Overloading in subclass only to get around a silly misimplemenation of classmethods is the ghost of versions past... I'm writing a framework that should do as much work as possible in the superclass, leaving only tasks specific the the subclass to be implemented in the subclass. Anything else is mickey mouse. I bet you can feel the resentment :-) feature requests are done via the bugs DB: http://bugs.php.net/ Great! But Torsten already is on this mission, it seems. take the time to thoroughly investigate the DB for similar requests first though, to avoid duplication. Also you may want to politely inquire at [EMAIL PROTECTED] before you do so - possibly they may have some clarification or could point you to earlier discussions that explain the current developer stance. There has to be a way to get this implemented into PHP 5... well if you can write C then definitely - you write it, and submit a patch - even if its not accepted you can roll your own version. :-) I write C well, but I'd rather stick to a standard distro then my own rolll. Besides, I just don't have the time to do this. Cheers, -Morten -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Jochem Maas wrote: Mehdi Achour wrote: Because it's a change, that should be reverted, or documented. don't top post - its bad form and many people ignore topposts. Mehdi is right to say that debug_backtrace() has changed - but so has the whole engine - I don't think that function was ever meant to be used in the way Morten was (or was it Torsten?). its clearly for debugging, no? Ah - but I was only checking out a previous tip on the subject. I wasn't actually trying to implement a fix using debug_backtrace(). Good heavens, no! It would be far to ineffective. and yeah, everything should be documented - but are you going to do? :-) didou ... why do you think they'd respond with something else than ask at php.generals ? bogus! - this is not a case of someone not knowing/understanding existing functionality. its a case of him missing something which quite a few people think should be available to PHP users, ofcourse the developers are entitled to disagree. actually I read php-dev like a zealot (brilliant way to learn about php) and I see the chap in question has posted his question and already got one answer... which didn't mention php-general at all ;-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
This should work for you then(maybe...i don't have php5 on my system, so it may not, but i think it would. http://us4.php.net/manual/en/function.get-class.php class Car { function drive() { return get_class($this); } } class Porshe { } $foo = new Porshe(); echo $foo-drive(); On Tue, 11 Jan 2005 14:24:46 -0500, Jason Barnett [EMAIL PROTECTED] wrote: __CLASS__ contains the name of the class the method is in. In my sample it would be 'Car' and not 'Porsche'. What I don't understand is why the behaviour of debug_backtrace() has been changed!?! Regards, Torsten I have no idea why the behaviour changed (I didn't really use the function before, either). And I see now the change in the manual that addresses this (essentially debug_backtrace gives you __CLASS__) If you had an actual instance of the class you could use get_class, but alas you are using a static method call. You punk. The only other solution that comes to mind is a little messy but it lets you get away with no object. Instead of calling the method statically you can use call_user_func_array() with the child class name as a parameter. Then change the parent method to accept the child class name as a parameter. ?php function call_static_child() { $drive_args = func_get_args(); /** assume that first parameter is child of class Car */ return call_user_func_array(array($drive_args[0], 'drive'), $drive_args); } ? -- Teach a person to fish... Ask smart questions: http://www.catb.org/~esr/faqs/smart-questions.html PHP Manual: http://www.php.net/manual/en/index.php php-general archives: http://marc.theaimsgroup.com/?l=php-generalw=2 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Christopher Fulton wrote: This should work for you then(maybe...i don't have php5 on my system, so it may not, but i think it would. http://us4.php.net/manual/en/function.get-class.php class Car { function drive() { return get_class($this); } } class Porshe { } $foo = new Porshe(); echo $foo-drive(); He needs a static function, ie Porche::drive() -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Christopher Fulton wrote: This should work for you then(maybe...i don't have php5 on my system, so it may not, but i think it would. http://us4.php.net/manual/en/function.get-class.php class Car { function drive() { return get_class($this); } } class Porshe { } $foo = new Porshe(); echo $foo-drive(); Chris, I was thinking of this as well. Except that the method drive is actually a static method so the class definition for Car is (I'm assuming) more like this: ?php class Car { static function drive() { /** I am a static function, so $this should not exist! */ /** However, class constants and static variables are available */ } } class Porsche extends Car { } Porsche::drive(); ? Torsten, I also found the following link to be helpful. Check out the user notes from michael at digitalgnosis dot removethis dot com (he did something similar to what I have already suggested, i.e. call_user_func) http://www.php.net/manual/en/language.oop5.static.php -- Teach a person to fish... Ask smart questions: http://www.catb.org/~esr/faqs/smart-questions.html PHP Manual: http://www.php.net/manual/en/index.php php-general archives: http://marc.theaimsgroup.com/?l=php-generalw=2 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
Daniel Schierbeck wrote: Christopher Fulton wrote: This should work for you then(maybe...i don't have php5 on my system, so it may not, but i think it would. http://us4.php.net/manual/en/function.get-class.php class Car { function drive() { return get_class($this); } } class Porshe { } $foo = new Porshe(); echo $foo-drive(); He needs a static function, ie Porche::drive() PHP 5.1 does have support for $this in static objects though ;) but there's some special case because of which it has that... can't remember what or how. I might be off on this entirely though aswell :P -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Get name of extending class with static method call
I was kinda thinking about all that too, for a project I'm currently doing, in that I wanted to be able to create a singleton, without having to put the singleton code in each class. The only hack I could think of was to use debug_backtrace() to get the line of source that contained the call, and parse(reparse) it manually. Bare in mind that this is EXTREMELY UGLY, and probably extremely slow, and unprofessional, but as a wise man once said(Andrew Morton IIRC), if there is no implemention there is nothing to improve on, but if you provide a crappy solution, you can rally people to improve on it. If you're new to PHP, or prone to picking up bad habits, stop reading now. eg ?pseudo_code class Base { function static_get_class_name(){ list($file, $line, $method) = get_calling_file_and_line_and_method_from_debug_backtrace(); $codeline = file($file)[$line]; preg_match(/([a-zA-Z_][a-zA-Z0-9_]*)::$method_name/, $codeline, $match); return $match[1]; } } On Tue, 11 Jan 2005 11:38:55 -0500, Jason Barnett [EMAIL PROTECTED] wrote: M. Sokolewicz wrote: try using __CLASS__ Torsten Roehr wrote: This is a good suggestion but I wonder... Torsten do you have a large heirarchy of parent classes or just one parent? E.g. Car - Sports Car - Porsche. More importantly will __CLASS__ resolve to the class name that you need... If __CLASS__ works for you then I would go with it. If not can you just send the appropriate class name as a parameter? -- Teach a person to fish... Ask smart questions: http://www.catb.org/~esr/faqs/smart-questions.html PHP Manual: http://php.net/manual/ php-general archives: http://marc.theaimsgroup.com/?l=php-generalw=2 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php