Inheritance and Static Methods

php.internals

Phil Dier

23 years ago
Is there a function that can be used to retrieve the name of a statically called child class from within an inherited method? debug_backtrace() works for this purpose, but I've seen people say that it is slow. Consider the example code below. Is this a planned feature for PHP 5, or could this possibly make it into 4.x? What options do I have? <? class a { function test_backtrace() { $bt = debug_backtrace(); echo "class pre: ".__CLASS__."\n"; echo "class bt: ".$bt[0]["class"]."\n"; echo "class get_class: ".get_class($this)."\n"; } } class b extends a { } echo "** static: \n"; b::test_backtrace(); $b = new b(); echo "** object: \n"; $b->test_backtrace(); ?> Output Looks Like: ** static: class pre: a class bt: b class get_class: ** object: class pre: a class bt: b class get_class: b Perhaps get_class() could return the expected class name when called without arguements?
-- Phil Dier gett communications <http://www.gettcomm.com/> gettlabs, inc. <http://www.gettlabs.com/>

(Marcus Börger)

23 years ago
Hello Phil, Tuesday, July 8, 2003, 11:11:49 PM, you wrote: PD> Is there a function that can be used to retrieve the name of a statically PD> called child class from within an inherited method? debug_backtrace() works PD> for this purpose, but I've seen people say that it is slow. Consider the PD> example code below. Is this a planned feature for PHP 5, or could this PD> possibly make it into 4.x? What options do I have? PD> <? PD> class a PD> { PD> function test_backtrace() PD> { PD> $bt = debug_backtrace(); PD> echo "class pre: ".__CLASS__."\n"; PD> echo "class bt: ".$bt[0]["class"]."\n"; PD> echo "class get_class: ".get_class($this)."\n"; PD> } PD> } PD> class b extends a PD> { PD> } PD> echo "** static: \n"; PD> b::test_backtrace(); PD> $b = new b(); PD> echo "** object: \n"; $b->>test_backtrace(); ?>> PD> Output Looks Like: PD> ** static: PD> class pre: a PD> class bt: b PD> class get_class: PD> ** object: PD> class pre: a PD> class bt: b PD> class get_class: b - the method is defined in class a so pre is correct - backtrace @[0] sees the top caller class that's b that's correct - get_class($var) sees the class of the variable. In you static case that is NULL which doesn't have a class and in your object case that's b. Therfore this is also correct. Everything show is correct so far. The only problem is that test_backtrace() could be called static without being defined as such. PD> Perhaps get_class() could return the expected class name when PD> called without arguements? This is nonsense. NULL does not have a class type. Best regards, Marcus mailto:helly@php.net

Phil Dier

23 years ago
On Tue, 8 Jul 2003 23:35:14 +0200 marcus.boerger@t-online.de (Marcus Börger) wrote: [snip]
> - the method is defined in class a so pre is correct > - backtrace @[0] sees the top caller class that's b that's correct > - get_class($var) sees the class of the variable. In you static case that is > NULL which doesn't have a class and in your object case that's b. Therfore > this is also correct. > > Everything show is correct so far. The only problem is that test_backtrace() > could be called static without being defined as such. >
[snip] Thanks for your response. I wasn't disputing the current behaviour, only wondering if there is / is planned a function specifically for doing what I did with debug_backtrace() in a static method. Using debug_backtrace() seems hackish and slow.
-- Phil Dier gett communications <http://www.gettcomm.com/> gettlabs, inc. <http://www.gettlabs.com/>