It is not possible to get the variables of callers, but you can get their arguments. This is easy to do with debug_backtrace() :
<?php class DebugTest { public static function testFunc($arg1, $arg2) { return self::getTrace(); } private static function getTrace() { $trace = debug_backtrace(); return sprintf( "This trace function was called by %s (%s:%d) with %d %s: %s\n", $trace[1]["function"], $trace[1]["file"], $trace[1]["line"], count($trace[1]["args"]), count($trace[1]["args"]) === 1 ? "argument" : "arguments", implode(", ", $trace[1]["args"]) ); } } echo DebugTest::testFunc("foo", "bar");
Running this program we get:
This trace function was called by testFunc (/Users/mike/debug.php:23) with 2 arguments: foo, bar
debug_backtrace() returns an array; element 0 is the function in which the function itself was called, so we use element 1 in the example. You can use a loop to completely move the trace.
source share