|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-06-16 19:33 UTC] gk at proliberty dot com
Description:
------------
debug_backtrace() should behave consistently in order to be useful in all contexts: inside classes, functions and from top level of script file.
However, when exectuted from top level, it returns an empty array.
According to the documentation, it should return an array with minimal info including __LINE__, __FILE__
>debug_backtrace() generates a PHP backtrace and returns this information as an associative array. The possible returned elements are listed in the following table:
...
line integer The current line number. See also __LINE__.
file string The current file name. See also __FILE__.
Reproduce code:
---------------
<?php print_r(debug_backtrace()); ?>
Expected result:
----------------
[greg@p3 xobj]$ php /tmp/a.php
array(1) {
[0]=>
array(4) {
["file"] => string(10) "/tmp/a.php"
["line"] => int(1)
["function"] =>
["args"]=>
}
}
Actual result:
--------------
[greg@p3 xobj]$ php /tmp/a.php
Array
(
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 06:00:02 2025 UTC |
Yes, below is an example of how I'm using it. The purpose is to work around the limited arguments passed to custom error handlers, installed with set_error_handler(), which do not receive class and function information. The following function replaces trigger_error() by talking directly to my custom error handler ('phpc_error_handler', omitted for brevity below), using debug_backtrace() to pass class and function info for prepending to error messages. <?php /////////////////////////////////////////////////////// /* phpc_trigger_error( $errorMessage,$errorCode,$debugBacktrace=NULL ) same as PHP built-in trigger_error, with optional parameter generate error message including debugging information USAGE: phpc_trigger_error( $errorMessage,$errorCode,debug_backtrace() ); */ ///////////////////////////////////////////////////////// function phpc_trigger_error( $errorMessage,$errorCode,$debug_backtrace=NULL ){ $errfile=__FILE__; $errline=__LINE__; $errclass=""; $errfunction=""; if (isset($debug_backtrace[0])){ $errfile=$debug_backtrace[0]['file']; $errline=$debug_backtrace[0]['line']; $errclass=$debug_backtrace[0]['class']; $errfunction=$debug_backtrace[0]['function']; } if (!empty($errclass)) $errclass.='::'; if (!empty($errfunction)) $errfunction.='(): '; $errorMessage=$errclass.$errfunction.$errorMessage.' '; phpc_error_handler ($errorCode,$errorMessage,$errfile,$errline); } // phpc_trigger_error() ?>I just realized that the expected result I entered is not consistent with how debug_backtrace() works: function debug_backtrace must also be listed. Here's a revision. Expected result: ---------------- [greg@p3 xobj]$ php /tmp/a.php Array( [0] => Array ( [file] => /tmp/a.php [line] => 1 [function] => debug_backtrace [args] => Array ( ) ) )