I’d like to create a debug function that dumps information about the execution context along with some other info.
In debug.php I have a function that dumps whatever is passed as a parameter. I’m interested in a way of calling the dump function from, say example.php and have it return the file name, the line number and the function name of the calling context.
1. <?php
2. function my_function(){
3. $var = 'My example';
4. dump( $var );
5. }
6. ?>
example.php
I’d like for the above function to output: “example.php, my_function, 4: My example”.
Is there anyway of doing this (short of passing __FILE__, __FUNCTION__, and __LINE__) as parameters?
Yes, you can use
debug_backtrace().It returns an array of all the callers and files (traces included files as well) in reverse order (highest ancestor last). So the caller would be the first in the array: