Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: determine where function was called from

is there a way to find out, where a function in PHP was called from? example:

function epic() {   fail(); }  function fail() {   //at this point, how do i know, that epic() has called this function? } 
like image 952
pol_b Avatar asked Jun 02 '10 19:06

pol_b


People also ask

How do you find where a function is defined?

If you use an IDE like Netbeans, you can CTRL+Click the function use and it will take you to where it is defined, assuming the file is within the project folder you defined.

How do you check function is called or not PHP?

isset($return)){ $return = findFunction($function, $inputDirectory."/". $folder); } else if($return == false){ $return = findFunction($function, $inputDirectory."/". $folder); } } } else { return false; } } findFunction("testFunction", "rootDirectory");

What is debug_ backtrace in PHP?

The debug_backtrace() function generates a PHP backtrace. This function displays data from the code that led up to the debug_backtrace() function. Returns an array of associative arrays.

How to pass parameter to PHP function?

PHP Function Arguments Information can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.


1 Answers

You can use debug_backtrace().

Example:

<?php  function epic( $a, $b ) {     fail( $a . ' ' . $b ); }  function fail( $string ) {     $backtrace = debug_backtrace();      print_r( $backtrace ); }  epic( 'Hello', 'World' ); 

Output:

Array (     [0] => Array         (             [file] => /Users/romac/Desktop/test.php             [line] => 5             [function] => fail             [args] => Array                 (                     [0] => Hello World                 )          )      [1] => Array         (             [file] => /Users/romac/Desktop/test.php             [line] => 15             [function] => epic             [args] => Array                 (                     [0] => Hello                     [1] => World                 )          )  ) 
like image 180
romac Avatar answered Sep 24 '22 00:09

romac