October 28, 2010

Find who calls a method in PHP's class

Question by balkon_smoke

Possible Duplicate:
Find out which class called a method in another class.

Hello everyone,

I have a class and I can’t find where his object creates.
I mean, can I somehow find out who calls his constructor?

Thanks a lot,
Andrey.

Answer by Starx

use

$trace = debug_backtrace();
echo "<pre>".print_r($trace[1])."</pre>"; 
//see all the displays '1' is the referrer '0' is self
echo $callingfunction = $trace[1]['function'];
echo $callingclass = $trace[1]['class'];

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!