I'm finding that sometimes debug_backtrace()
is not including the line number for a call. Is there some reason why this is and any way to correct for it?
Thanks in advance.
P.S. And yes, the calls that it is omit line numbers for are my own code, not internal PHP code.
For more advanced solution, you can use XDebug extension for PHP. By default when XDebug is loaded, it should show you automatically the backtrace in case of any fatal error. Or you trace into file (xdebug. auto_trace) to have a very big backtrace of the whole request or do the profiling (xdebug.
But, what is a stack trace? In essence, it is a rundown of every file and function that is called leading up to the error. To be clear, a stack trace doesn't include the files and functions that are touched before the error occurred, only the chain of methods that are called as the error happened.
Definition and UsageThe debug_backtrace() function generates a PHP backtrace. This function displays data from the code that led up to the debug_backtrace() function. The current call type.
To print a backtrace of the entire stack, use the backtrace command, or its alias bt . This command will print one line per frame for frames in the stack. By default, all stack frames are printed. You can stop the backtrace at any time by typing the system interrupt character, normally Ctrl-c .
Consider following code:
<?
class BtTest
{
public function getTheItem()
{
var_dump( debug_backtrace( false ) );
$bt = debug_backtrace( false );
return $bt[1];
}
public function __call( $methodName, $methodArgs )
{
return $this->getTheItem();
}
}
$o = new BtTest();
$bti = $o->test();
assert( 'array_key_exists("function", $bti)' );
assert( 'array_key_exists("line", $bti)' );
assert( 'array_key_exists("file", $bti)' );
The execution of above example generates following output:
array(3) {
[0]=>
array(6) {
["file"]=>
string(53) "/somewhere/in/the/filesystem/tests/bt-test-so.php"
["line"]=>
int(13)
["function"]=>
string(10) "getTheItem"
["class"]=>
string(6) "BtTest"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
[1]=>
array(4) {
["function"]=>
string(6) "__call"
["class"]=>
string(6) "BtTest"
["type"]=>
string(2) "->"
["args"]=>
array(2) {
[0]=>
&string(4) "test"
[1]=>
&array(0) {
}
}
}
[2]=>
array(6) {
["file"]=>
string(53) "/somewhere/in/the/filesystem/tests/bt-test-so.php"
["line"]=>
int(18)
["function"]=>
string(4) "test"
["class"]=>
string(6) "BtTest"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
}
PHP Warning: assert(): Assertion "array_key_exists("line", $bti)" failed in /somewhere/in/the/filesystem/tests/bt-test-so.php on line 21
PHP Warning: assert(): Assertion "array_key_exists("file", $bti)" failed in /somewhere/in/the/filesystem/tests/bt-test-so.php on line 22
The first backtrace item (index 0) says indirectly (through the line
and file
items) that the getTheItem
method was called from the __call
method.
The second backtrace item (index 1) says that the __call
method was called from somewhere (missing line
and file
items).
The third backtrace item (index 2) says that the test
method was called from the global scope of the script.
The place of the __call
method call is probably in some method resolution code somewhere in the php interpreter code. There are two possibilities of fixing it. Either the second item should refer interpreter's source code file and line or the second and the third backtrace items should be merged into one. I personally would prefer the second solution as the interpreter's internals are not interesting for me (this is how they seem to do it in python's traceback), however I understand that sometimes the first solution provides more explicit trace (especially when it's a callback which is called from the internals).
So or so, it seems that the developer(s) responsible for (or at least maintaining) the code of the debug_backtrace
function doesn't perceive it as a bug or maybe has no easy way to fix it. It would be ok to fill the line
and file
items with some place holder values (e.g. <unknown-file>
and 0
or even nulls) and emphasize it in the documentation. Unless someone will successfully convince them to do it, you just have to handle the special case in your code.
I wrote above just to share my understanding of the strange behaviour of the function. If someone has a willingness to fight for a slightly better world, here are links to some related bug reports:
__FILE__
, __LINE__
The oldest report is from 2003, so you shouldn't count on a fast fix :)
I think this is listed as a PHP Bug
The debug backtrace shows filename and lineno of calling script. In case function is called from inside internal function (may be as callback) no filename and lineno may be set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With