0

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.

Community
  • 1
  • 1
balkon_smoke
  • 1,136
  • 2
  • 10
  • 25
  • @Gordon: I think this question differs a bit because of "I have a class and I can't find where his object creates" and therefore (imo) debug_backtrace() isn't the single best answer. You shouldn't _change_ existing code to get a class/dependency/call graph. – VolkerK Oct 28 '10 at 16:02
  • @VolkerK actually debug_backtrace is the worst answer. If the aim of the OP is to know the caller in the callee, then the caller should be passed to the callee and not the callee trying to find out the caller from the backtrace. And you are right suggesting a debugger/profiler for your reading of the question. – Gordon Oct 28 '10 at 16:09

2 Answers2

1

You can use debug_backtrace() or even better a tracer/profiler like Xdebug to gather the information and e.g. KCachegrind to visualize it.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
1

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'];
Starx
  • 77,474
  • 47
  • 185
  • 261