0

I would like to know if there is a way to access information when a method is called from inside a class.

Example.

Here I have a class, with a __classStatic() method, to generate arbitrary methods.

class Caller
{
  public function __callStatic($func, $args){
       echo "method $func is being called from class ..."; 
  }
}

Now, Whenever a method is called ex:Caller::foo() inside say.. a class named Reader,

class Reader{
    Caller::foo(); 
}

I would like `foo() to output.

method foo is being called from class Reader

So, the problem is getting the call name from which a method is being called.

I tried inside foo() to get the class name using:

get_class()
get_parent_class()
get_called_class()

But, none of provide any information that I am looking for.

Community
  • 1
  • 1
robue-a7119895
  • 816
  • 2
  • 11
  • 31

1 Answers1

0

There is no language construct in PHP to achieve this. To do it you would need to analyze the stack trace:

class Callee
{
    public static function __callStatic($func, $args){
        $trace = debug_backtrace();
        for($i = 1; $i < count($trace); $i++) {
            $x = $trace[$i];
            if($x['function'] === $func && is_a($x['class'], __CLASS__, true)) {
                continue;
            }
            $class = $x['class'];
            break;
        }
        return "method $func is being called from class $class";
    }
}

class Foo extends Callee
{
}

class Reader
{
  function doSomething(){
      echo Foo::foo();
  }
}

(new Reader)->doSomething();

Output:

method foo is being called from class Reader

However, this performs very bad. Your application logic should not rely on this. For analytical purposes you might use a debugger.


Original answer, given because you first wrote that you expect:

method foo is being called from class Reader

You need to use get_called_class():

class Caller
{
  public static function __callStatic($func, $args){
       echo "method $func is being called from class " . get_called_class();
  }
}

Also note, that __callStatic() needs to be declared static. Btw, the class name Caller sounds weird. I guess you meant Callee.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • The output of this will be `Caller` while OP wants it to be `Reader`. That seems like an interesting question. – thpl Jul 08 '14 at 12:49
  • 1
    Damn! he edited the question after I answered!!!! Before he wroted `Caller` is expected. – hek2mgl Jul 08 '14 at 12:52
  • @hek2mgl Sorry :) I didn't change the question overall. Just some logic – robue-a7119895 Jul 08 '14 at 12:54
  • 1
    @CONtext You *completely annihilated* the meaning of the question. Next time, ask what you *mean* to ask so you don't end up wasting people's time like this. – Niet the Dark Absol Jul 08 '14 at 12:56
  • @NiettheDarkAbsol You are right. But check the example of my last comment to see the difference between `__CLASS__` and `get_called_class()` in action – hek2mgl Jul 08 '14 at 12:59
  • @hek2mgl Yeah, this does answer the question but as you stated, it does seem very bad, and sloppy approach. I don't like foo extending Caller at all. but +1 until someone can give a better answer (if any) thanks – robue-a7119895 Jul 08 '14 at 13:14
  • 1
    I just wanted to show that I took inheritance into account. Maybe you even don't want that in real life scenarios. However, a better way would be just pass the calling class as an argument to the function. – hek2mgl Jul 08 '14 at 13:16