0

we have one class

class A
{
    // I am using class B here
   public function whatever()
   {
      $class_b = new B;
      $class_b->show_caller();
   }
}

and other

class B
{
    public function show_caller()
    {
        // show me the caller class (should output "A")
    }

}

I need to get the caller class name. P.S. Inheritance is not an option!

remtsoy
  • 465
  • 1
  • 3
  • 15
  • 1
    Why do you need the caller? Sounds like a design-flaw. – tkausl Aug 21 '16 at 15:09
  • Possible duplicate of [Print PHP Call Stack](http://stackoverflow.com/questions/1423157/print-php-call-stack) – Andrew Sun Aug 21 '16 at 15:10
  • Well, I know the architecture is bad, but project it's pretty big and i haven't time to refactor all of it. I just can't use Inheritance. – remtsoy Aug 21 '16 at 15:11

2 Answers2

1

Pass class A as a parameter to B::show_caller() and output it's class name using get_class():

class A
{
    // I am using class B here
   public function whatever()
   {
      $class_b = new B;
      $class_b->show_caller($this);
   }
}

class B
{
    public function show_caller($class)
    {
        echo get_class($class);
    }

}
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • That doesn't mean anything. *How* is it supposed to be dynamic? How wouldn't solution work for you? – John Conde Aug 21 '16 at 15:13
  • I think he mean that $class_b->show_caller($class_b); will return B, not A. But it's really interesting to find solution without parameters – jaro1989 Aug 21 '16 at 16:25
0

We can't provide the knowledge about of where your method is executing to method's class without parameters. Late static binding will work only with inheritance, so it's not your case. The only approach, I can give you, is to use your IDE ability to find method usage in your project (ctrl+mouse1 in PhpStorm). If you're not using any IDE - it's a time to start.

jaro1989
  • 405
  • 8
  • 15