Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Consider following code:</p> <pre><code>&lt;? class BtTest { public function getTheItem() { var_dump( debug_backtrace( false ) ); $bt = debug_backtrace( false ); return $bt[1]; } public function __call( $methodName, $methodArgs ) { return $this-&gt;getTheItem(); } } $o = new BtTest(); $bti = $o-&gt;test(); assert( 'array_key_exists("function", $bti)' ); assert( 'array_key_exists("line", $bti)' ); assert( 'array_key_exists("file", $bti)' ); </code></pre> <p>The execution of above example generates following output:</p> <pre><code>array(3) { [0]=&gt; array(6) { ["file"]=&gt; string(53) "/somewhere/in/the/filesystem/tests/bt-test-so.php" ["line"]=&gt; int(13) ["function"]=&gt; string(10) "getTheItem" ["class"]=&gt; string(6) "BtTest" ["type"]=&gt; string(2) "-&gt;" ["args"]=&gt; array(0) { } } [1]=&gt; array(4) { ["function"]=&gt; string(6) "__call" ["class"]=&gt; string(6) "BtTest" ["type"]=&gt; string(2) "-&gt;" ["args"]=&gt; array(2) { [0]=&gt; &amp;string(4) "test" [1]=&gt; &amp;array(0) { } } } [2]=&gt; array(6) { ["file"]=&gt; string(53) "/somewhere/in/the/filesystem/tests/bt-test-so.php" ["line"]=&gt; int(18) ["function"]=&gt; string(4) "test" ["class"]=&gt; string(6) "BtTest" ["type"]=&gt; string(2) "-&gt;" ["args"]=&gt; 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 </code></pre> <p>The first backtrace item (index 0) says indirectly (through the <code>line</code> and <code>file</code> items) that the <code>getTheItem</code> method was called from the <code>__call</code> method.</p> <p>The second backtrace item (index 1) says that the <code>__call</code> method was called from <em>somewhere</em> (missing <code>line</code> and <code>file</code> items).</p> <p>The third backtrace item (index 2) says that the <code>test</code> method was called from the global scope of the script.</p> <p>The place of the <code>__call</code> 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).</p> <p>So or so, it seems that the developer(s) responsible for (or at least maintaining) the code of the <code>debug_backtrace</code> function doesn't perceive it as a bug or maybe has no easy way to fix it. It would be ok to fill the <code>line</code> and <code>file</code> items with some place holder values (e.g. <code>&lt;unknown-file&gt;</code> and <code>0</code> or even <em>nulls</em>) and emphasize it in the documentation. <em>Unless someone will successfully convince them to do it, you just have to handle the special case in your code.</em></p> <p>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:</p> <ul> <li><a href="https://bugs.php.net/bug.php?id=39070" rel="noreferrer">#39070 debug_backtrace output when call_user_func or error handler involved</a></li> <li><a href="https://bugs.php.net/bug.php?id=24214" rel="noreferrer">#24214 debug_backtrace() fails to report <code>__FILE__</code>, <code>__LINE__</code></a></li> <li><a href="https://bugs.php.net/bug.php?id=24405" rel="noreferrer">#24405 debug_backtrace - missing info</a></li> <li><a href="https://bugs.php.net/bug.php?id=38047" rel="noreferrer">#38047 "file" and "line" sometimes not set in backtrace from inside error handler</a></li> <li><a href="https://bugs.php.net/bug.php?id=44428" rel="noreferrer">#44428 "file" and "line" missing in debug_backtrace() output</a></li> </ul> <p>The oldest report is from 2003, so you shouldn't count on a fast fix :) </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload