How can I get the current function's caller function's arguments?
            Asked
            
        
        
            Active
            
        
            Viewed 535 times
        
    2
            
            
        - 
                    1If your using `debug_backtrace()`, this provides the arguments - *args array If inside a function, this lists the functions arguments.* – Nigel Ren Oct 13 '18 at 09:55
2 Answers
1
            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"
  }
}
 
    
    
        Madhur Bhaiya
        
- 28,155
- 10
- 49
- 57
1
            
            
        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');
 
    
    
        Parsa
        
- 596
- 1
- 5
- 15
