PHP5: expected __METHOD__ behavior

php.internals

Daniel J Cain Jr .

22 years ago
I am by no means an expert with OOP, so if this is a blatantly retarded question please excuse my ignorance. Given this code: <?php class A { function foo() { echo __METHOD__; } } class B { function bar() { // blah } } $instance = B::foo(); ?> output is "A::foo". Is this correct? I would expect (want maybe :) ) to see output as "B::foo". If you need more information please let me know. TIA -dan

l0t3k

22 years ago
"Daniel J Cain Jr ." <dan@cain.sh> wrote in message news:FBCF2135-AB83-11D8-A867-000A95BE1F8C@cain.sh...
> > $instance = B::foo(); > ?> > > output is "A::foo". > > Is this correct? I would expect (want maybe :) ) to see output as > "B::foo".
this is as designed. __METHOD__ is a compile-time evaluated token, not a runtime evaluated function. l0t3k

Andrey Hristov

22 years ago
Daniel J Cain Jr. wrote:
> I am by no means an expert with OOP, so if this is a blatantly retarded > question please excuse my ignorance. > > Given this code: > > <?php > class A > { > function foo() > { > echo __METHOD__; > } > } > > class B > { > function bar() > { > // blah > } > } > > $instance = B::foo(); > ?> > > output is "A::foo". > > Is this correct? I would expect (want maybe :) ) to see output as > "B::foo". If you need more information please let me know. > > TIA > > -dan >
Hi, this "constant" like the others is resolved on compile time. Another example is __CLASS__: php -r 'class a{ function c() {var_dump(__CLASS__);}}class b extends a{};$a=new b();$a->c();' dumps : string(1) "a" __METHOD__ is resolved as __CLASS__.'::'.__FUNCTION__ IMO the current behaviour is the normal since works like the C preprocessor doing substitutions before compile (PHP does it while compiling). You can get what you wanted with this : echo get_class($this).'::'.__FUNCTION__ (when there is an instance of the class) but AFAIK in your case with static calls there is no solution. Cheers, Andrey

Johannes Schlueter

22 years ago
Hi, Andrey Hristov wrote:
> echo get_class($this).'::'.__FUNCTION__ (when there is an instance of the > class) but AFAIK in your case with static calls there is no solution.
Is there some way to add a function (or some other magic thing) that works with static calls and call it a bug fix, so it can be in 5.0? If not: Is there a way to implement it and add it as a new feature with PHP5.1? Lately I needed such a thing while building some kind of framework. I solved it by adding a function to my extended class which passes it's __CLASS__ as an additional paramter to my base function - I don't really like it that way ;-) johannes

Sara Golemon

22 years ago
debug_backtrace(); Not the prettiest solution, but a reliable one nonetheless. -Sara "Johannes Schlueter" <schlueter@phpbar.de> wrote in message news:20040522084054.30545.qmail@pb1.pair.com...
> Hi, > > Andrey Hristov wrote: > > echo get_class($this).'::'.__FUNCTION__ (when there is an instance of
the
> > class) but AFAIK in your case with static calls there is no solution. > > Is there some way to add a function (or some other magic thing) that works > with static calls and call it a bug fix, so it can be in 5.0? If not: Is > there a way to implement it and add it as a new feature with PHP5.1? > Lately I needed such a thing while building some kind of framework. I
solved
> it by adding a function to my extended class which passes it's __CLASS__
as

Johannes Schlueter

22 years ago
Hi, Not really, too. <?php class a { static public function foo() { print_r(debug_backtrace()); } } class b extends a {} a::foo(); b::foo(); ?> Array ( [0] => Array ( [file] => - [line] => 8 [function] => foo [class] => a [type] => :: [args] => Array ( ) ) ) Array ( [0] => Array ( [file] => - [line] => 8 [function] => foo [class] => a [type] => :: [args] => Array ( ) ) ) in both cases it tells [class] => a, so one still needs to extend the foo()-Method: <?php class a { static public function foo() { print_r(debug_backtrace()); } } class b extends a { static public function foo() { parent::foo(); } } a::foo(); b::foo(); ?> Array ( [0] => Array ( [file] => - [line] => 15 [function] => foo [class] => a [type] => :: [args] => Array ( ) ) ) Array ( [0] => Array ( [file] => - [line] => 11 [function] => foo [class] => a [type] => :: [args] => Array ( ) ) [1] => Array ( [file] => - [line] => 16 [function] => foo [class] => b [type] => :: [args] => Array ( ) ) ) Asyou can see at $bt = debug_backtrace(); $bt[1]['class'] the name is available - but only if the function wasextended like in the example.... johannes Sara Golemon wrote: