Reputation: 56391
How can I get the current function's caller function's arguments?
Upvotes: 2
Views: 999
Reputation: 644
The topic you mentioned with your answer https://stackoverflow.com/a/9133897/3224296
function GetCallingMethodName(){
$e = new Exception();
$trace = $e->getTrace();
//position 0 would be the line that called this function so we ignore it
$last_call = $trace[1];
print_r($last_call);
}
function firstCall($a, $b){
theCall($a, $b);
}
function theCall($a, $b){
GetCallingMethodName();
}
firstCall('lucia', 'php');
Upvotes: 1
Reputation: 28834
Use debug_backtrace
function.
It generates a PHP backtrace, returning an array of associative arrays. One of the keys in those associative arrays is 'args'
. If called inside a function, this key basically contains the functions arguments list (as an array). If this is used inside an included file, this lists the included file name(s).
For eg (from PHP docs):
function a_test($str)
{
echo "\nHi: $str";
var_dump(debug_backtrace());
}
a_test('friend');
It will output the following:
array(2) {
[0]=>
array(4) {
["file"] => string(10) "/tmp/a.php"
["line"] => int(10)
["function"] => string(6) "a_test"
["args"]=>
array(1) {
[0] => &string(6) "friend"
}
}
[1]=>
array(4) {
["file"] => string(10) "/tmp/b.php"
["line"] => int(2)
["args"] =>
array(1) {
[0] => string(10) "/tmp/a.php"
}
["function"] => string(12) "include_once"
}
}
Upvotes: 1