欢迎转载,请注明作者及出处
1st Way:Use PHP methoddebug_backtrace
and/ordebug_print_backtrace
to generate a backtrace.
though those 2 methods you will get an array like this one :
array(2) {
[0]=>
array(4) {
["file"] => string(10) "/tmp/a.php"
["line"] => int(10)
["function"] => string(6) "a_test"
["args"]=>
array(1) {
[0] => &string(6) "friend"
}
}
[1]=>
array(4) {
["file"] => string(10) "/tmp/b.php"
["line"] => int(2)
["args"] =>
array(1) {
[0] => string(10) "/tmp/a.php"
}
["function"] => string(12) "include_once"
}
}
Note:They will apparently not flush the I/O buffer, but you can do that yourself, withflush
and/orob_flush
.
2nd Way: Exception::getTraceAsString()
When got an exception we can use instance of Exception to instead ofdebug_backtrace
. It's morereadable.
For example:
$e = new Exception;
var_dump($e->getTraceAsString());
#2 /usr/share/php/PHPUnit/Framework/TestCase.php(626): SeriesHelperTest->setUp()
#3 /usr/share/php/PHPUnit/Framework/TestResult.php(666): PHPUnit_Framework_TestCase->runBare()
#4 /usr/share/php/PHPUnit/Framework/TestCase.php(576): PHPUnit_Framework_TestResult->run(Object(SeriesHelperTest))
#5 /usr/share/php/PHPUnit/Framework/TestSuite.php(757): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#6 /usr/share/php/PHPUnit/Framework/TestSuite.php(733): PHPUnit_Framework_TestSuite->runTest(Object(SeriesHelperTest), Object(PHPUnit_Framework_TestResult))
#7 /usr/share/php/PHPUnit/TextUI/TestRunner.php(305): PHPUnit_Framework_TestSuite->run(Object(PHPUnit_Framework_TestResult), false, Array, Array, false)
#8 /usr/share/php/PHPUnit/TextUI/Command.php(188): PHPUnit_TextUI_TestRunner->doRun(Object(PHPUnit_Framework_TestSuite), Array)
#9 /usr/share/php/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#10 /usr/bin/phpunit(53): PHPUnit_TextUI_Command::main()
#11 {main}"
If you need to do more complex formatting,use function defined below to format output dump_value or you can get a format string by requirement:
/**
* format output dump_value or you can get a format string by requirement
* @param type $var
* @param type $echo
* @param type $label
* @param type $strict
* @return string
*/
function formatDump($var, $echo=true,$label=null, $strict=true)
{
$label = ($label===null) ? '' : rtrim($label) . ' ';
//If needn't strict
if(!$strict) {
if (ini_get('html_errors')) {
$output = print_r($var, true);
$output = "<pre>".$label.htmlspecialchars($output,ENT_QUOTES)."</pre>";
} else {
$output = $label . " : " . print_r($var, true);
}
}else {
//Turn on output buffering
ob_start();
//output it to buffer
var_dump($var);
//Get current buffer contents and delete current output buffer
$output = ob_get_clean();
//Find out whether 'xdebug' extension is loaded
if(!extension_loaded('xdebug')) {
//'m' means enhanced line anchor
$output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
$output = '<pre>'. $label. htmlspecialchars($output, ENT_QUOTES). '</pre>';
}
}
//if output on response
if ($echo) {
echo($output);
return null;
}else
return $output;
}//end if
reference link:http://stackoverflow.com/questions/1423157/print-php-call-stack