# PHP5: expected __METHOD__ behavior

- Thread: https://externals.io/message/10018
- Original archive: https://news-web.php.net/php.internals/10018
- Started by Daniel J Cain Jr . on 2004-05-22T00:07:16+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #10018 — Daniel J Cain Jr . — 2004-05-22T00:07:16+00:00

URL: https://externals.io/message/10018#10018

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

---

## #10019 — l0t3k — 2004-05-22T00:24:03+00:00

URL: https://externals.io/message/10018#10019 · In reply to: #10018

"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

---

## #10020 — Andrey Hristov — 2004-05-22T00:33:52+00:00

URL: https://externals.io/message/10018#10020 · In reply to: #10018

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

---

## #10022 — Johannes Schlueter — 2004-05-22T08:40:47+00:00

URL: https://externals.io/message/10018#10022 · In reply to: #10020

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

---

## #10034 — Sara Golemon — 2004-05-22T18:45:04+00:00

URL: https://externals.io/message/10018#10034 · In reply to: #10022

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:
> > […]
the
> > […]
>
> 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

---

## #10035 — Johannes Schlueter — 2004-05-22T19:32:58+00:00

URL: https://externals.io/message/10018#10035 · In reply to: #10034

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:

> `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...
> > […]
> the
> > […]
> solved
> > […]
> as
> > […]
