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();
Exception::getTrace
(PHP 5 >= 5.1.0)
Exception::getTrace — Récupère la trace de la pile
Description
final public array Exception::getTrace
( void
)
Retourne la trace de la pile de l'exception.
Liste de paramètres
Cette fonction ne contient aucun paramètre.
Valeurs de retour
Retourne la trace de la pile de l'exception sous la forme d'un tableau.
Exemples
Exemple #1 Exemple avec Exception::getTrace()
<?php
function test() {
throw new Exception;
}
try {
test();
} catch(Exception $e) {
var_dump($e->getTrace());
}
?>
L'exemple ci-dessus va afficher quelque chose de similaire à :
array(1) {
[0]=>
array(4) {
["file"]=>
string(22) "/home/bjori/tmp/ex.php"
["line"]=>
int(7)
["function"]=>
string(4) "test"
["args"]=>
array(0) {
}
}
}
andreas at cap-systems dot com
09-Jun-2010 12:55
