tags:

views:

255

answers:

3

Is there a way in PHP to find out what object called what method in another object.

Exmaple:

class Foo
{
  public function __construct()
  {
    $bar = new Bar();
    $bar->test();
  }
}

class Bar
{
  public function test()
  {
  }
}
$foo = new Foo();

Would there be a way for me to find out that the test method was called from the foo object?

+2  A: 

You can probably achieve this with a debug backtrace, though this seems kind of hackish.

Your alternative option is to pass a parameter to that class and tell it where it is being called from, when you instantiate the class from within another.

yuval
I've looked into this before and it seemed at the time that a backtrace was the only option.
Unkwntech
+1  A: 

At the very least, you could use debug_backtrace and analyze that to find the calling method.

I think you should also be able to do it using the reflection API, but it's been too long since I've used PHP and I don't remember exactly how. The links should at least get you started, however.

Randolpho
+3  A: 

Hi,

you could use debug_backtrace, a bit like this :
BTW, take a look at the comments on the manual page : there are some useful functions and advices given ;-)

class Foo
{
  public function __construct()
  {
    $bar = new Bar();
    $bar->test();
  }
}

class Bar
{
  public function test()
  {
      $trace = debug_backtrace();
      if (isset($trace[1])) {
          // $trace[0] is ourself
          // $trace[1] is our caller
          // and so on...
          var_dump($trace[1]);

          echo "called by {$trace[1]['class']} :: {$trace[1]['function']}";

      }
  }
}
$foo = new Foo();

The var_dump would output :

array
  'file' => string '/home/squale/developpement/tests/temp/temp.php' (length=46)
  'line' => int 29
  'function' => string '__construct' (length=11)
  'class' => string 'Foo' (length=3)
  'object' => 
    object(Foo)[1]
  'type' => string '->' (length=2)
  'args' => 
    array
      empty

and the echo :

called by Foo :: __construct

But, as nice as it might look like, I am not sure it should be used as a "normal thing" in your application... Seems odd, actually : with a good design, a method should not need to know what called it, in my opinion.

Pascal MARTIN
Working examples are always nice. +1. :)
Randolpho
Thanks ;-) (They are also a reason to not get the first answer ^^ but, at least, I have a bit of fun too, that way ;-) )
Pascal MARTIN
Thank you for the example.Yes, I agree to a certain extent, that methods should not need to know, but I have some other people writing code on top of what I am writing and I wanted to make their life easier, he he.I think using debug is a bit hackish, but thank you, you have given me a good insight into the debugging function, something id never really done before.
Botto
You're welcome :-) and thanks for explaining the reason why you needed this ! (Another solution could be to consider using a debugger, like the one provided by Xdebug, that can be integrated with Eclipse PDT ; see for instance http://stackoverflow.com/questions/1199342/how-to-debug-a-php-application and its answers)
Pascal MARTIN