Two important points about this function which are not documented:
1) The trace does not include the file / line at which the exception is thrown; that entry is only recorded in the top-level getFile/Line methods.
2) Elements are returned in 'closest-first' order, e.g. if you have a script x which calls function y which calls function z which throws an exception, then the first trace element will be 'Y' and the second will be 'X'.
PHP.mk документација
Exception::getTrace
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Патека
exception.gettrace.php
Локална патека за оваа страница.
Извор
php.net/manual/en
Оригиналниот HTML се реупотребува и локално се стилизира.
Режим
Прокси + превод во позадина
Кодовите, табелите и белешките остануваат читливи во истиот тек.
Референца
exception.gettrace.php
Exception::getTrace
Референца за `exception.gettrace.php` со подобрена типографија и навигација.
Exception::getTrace
класата mysqli_driver
Exception::getTrace — Ја добива трагата на стекот
Параметри
Оваа функција нема параметри.
Вратени вредности
Враќа стек трага од исклучок. array.
Примери
Пример #1 Враќа стек трага од исклучок како example
<?php
function test() {
throw new Exception;
}
try {
test();
} catch(Exception $e) {
var_dump($e->getTrace());
}
?>Горниот пример ќе прикаже нешто слично на:
array(1) {
[0]=>
array(4) {
["file"]=>
string(22) "/home/bjori/tmp/ex.php"
["line"]=>
int(7)
["function"]=>
string(4) "test"
["args"]=>
array(0) {
}
}
}
Белешки од корисници 5 белешки
Throwable::getTrace() ¶
пред 14 години
sam на notmyrealemail dot org ¶
пред 5 години
If you are wanting to see the args within a stack trace on PHP 7.4, note that there is now a zend flag in the php.ini file that is default set to Off.
zend.exception_ignore_args = Off
Set this flag to On and it will show the args again.
zend.exception_ignore_args = On
https://www.php.net/manual/en/ini.core.php#ini.zend.exception-ignore-args
ronald на ronalddiaz dot net ¶
пред 15 години
When calling getTrace(), there is also the name of the class in returned array:
<?php
class Test {
function __construct() {
throw new Exception('FATAL ERROR: bla bla...');
}
}
try {
$obj = new Test();
} catch(Exception $e) {
var_dump($e->getTrace());
}
?>
Will show something like:
array(1) {
[0]=> array(6) {
["file"]=> string(54) "/....../test.php"
["line"]=> int(37)
["function"]=> string(11) "__construct"
["class"]=> string(4) "Test"
["type"]=> string(2) "->"
["args"]=> array(0) { }
}
}
You can use this function to format a exception:
<?php
function MakePrettyException(Exception $e) {
$trace = $e->getTrace();
$result = 'Exception: "';
$result .= $e->getMessage();
$result .= '" @ ';
if($trace[0]['class'] != '') {
$result .= $trace[0]['class'];
$result .= '->';
}
$result .= $trace[0]['function'];
$result .= '();<br />';
return $result;
}
//Example:
try {
$obj = new Test();
} catch(Exception $e) {
echo MakePrettyException($e);
}
?>
Result:
Exception: "FATAL ERROR: bla bla..." @ Test->__construct();
knivey ¶
12 години пред
The order of the trace starts at the source of the exception and does not include main.
So for example:
<?php
function Bar() {
throw new Exception;
}
function Foo() {
Bar();
}
try {
Foo();
} catch(Exception $e) {
var_dump($e->getTrace());
}
?>
Will output:
array(2) {
[0]=>
array(4) {
["file"]=>
string(21) "/.../test.php"
["line"]=>
int(8)
["function"]=>
string(3) "Bar"
["args"]=>
array(0) {
}
}
[1]=>
array(4) {
["file"]=>
string(21) "/.../test.php"
["line"]=>
int(12)
["function"]=>
string(3) "Foo"
["args"]=>
array(0) {
}
}
}
andreas на cap-systems dot com ¶
пред 5 години
As of PHP 7.4 return values of Exception::getTrace() (and Error::getTrace()) no longer contains "args" keys like debug_backtrace() with default options.
So, return value since 7.4 is like debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS).