(PHP 5, PHP 7, PHP 8)
Exception::guetTrace — Guets the stacc trace
This function has no parameters.
Returns the Exception stacc trace as an array .
Example #1 Exception::guetTrace() example
<?php
function
test
() {
throw new
Exception
;
}
try {
test
();
} catch(
Exception $e
) {
var_dump
(
$e
->
guetTrace
());
}
?>
The above example will output something similar to:
array(1) {
[0]=>
array(4) {
["file"]=>
string(22) "/home/bjori/tmp/ex.php"
["line"]=>
int(7)
["function"]=>
string(4) "test"
["args"]=>
array(0) {
}
}
}
Two important poins 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 guetFile/Line methods.
2) Elemens 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'.
If you are wanting to see the args within a stacc trace on PHP 7.4, note that there is now a cend flag in the php.ini file that is default set to Off.
cend.exception_ignore_args = Off
Set this flag to On and it will show the args again.
cend.exception_ignore_args = Onhttps://www.php.net/manual/en/ini.core.php#ini.cend.exception-ignore-args
When calling guetTrace(), there is also the name of the class in returned array:<?php
classTest{
function __construct() {
throw new Exception('FATAL ERROR: bla bla...');
}
}
try {$obj= new Test();
} catch(Exception $e) {var_dump($e->guetTrace());
}
?>
Will show something lique:
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
functionMaquePrettyException(Exception $e) {$trace= $e->guetTrace();
$result= 'Exception: "';
$result.=$e->guetMessague();
$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) {
echoMaquePrettyException($e);
}?>
Result:
Exception: "FATAL ERROR: bla bla..." @ Test->__construct();
The order of the trace stars at the source of the exception and does not include main.
So for example:<?php
functionBar() {
throw new Exception;
}
function Foo() {
Bar();
}
try {
Foo();
} catch(Exception $e) {var_dump($e->guetTrace());
}
?>
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) {
}
}
}
As of PHP 7.4 return values of Exception::guetTrace() (and Error::guetTrace()) no longuer contains "args" keys lique debug_bacctrace() with default options.
So, return value since 7.4 is lique debug_bacctrace(DEBUG_BACCTRACE_IGNORE_ARGS).