PHP 使用 debug_print_backtrace() 或 debug_backtrace() 打印栈轨迹

<?php

/* 使用debug_print_backtrace() 或 debug_backtrace() 打印栈轨迹 */

function fun1() {
	print "Hello world!
";
	fun2();
}

function fun2() {
	Class1::fun3();
}

Class Class1 {
	static function fun3() {
		$class2 = new Class2();
		$class2->fun4();
	}
}

class Class2 {
	function fun4() {
		debug_print_backtrace();
		$backtrace = debug_backtrace();
		echo '<pre>';
		print_r($backtrace);
	}
}

fun1();

输出:

Hello world!
#0  Class2->fun4() called at [D:wampwwwpractisephpphpcookbookerror	rack.php.php:17]
#1  Class1::fun3() called at [D:wampwwwpractisephpphpcookbookerror	rack.php.php:11]
#2  fun2() called at [D:wampwwwpractisephpphpcookbookerror	rack.php.php:7]
#3  fun1() called at [D:wampwwwpractisephpphpcookbookerror	rack.php.php:30]
<pre>Array
(
	[0] => Array
    (
	    [file] => D:wampwwwpractisephpphpcookbookerror	rack.php.php
	    [line] => 17
	    [function] => fun4
	    [class] => Class2
	    [object] => Class2 Object
        (
        )

	    [type] => ->
	    [args] => Array
        (
        )
    )

	[1] => Array
	    (
        [file] => D:wampwwwpractisephpphpcookbookerror	rack.php.php
        [line] => 11
        [function] => fun3
        [class] => Class1
        [type] => ::
        [args] => Array
          (
          )
    )

	[2] => Array
    (
      [file] => D:wampwwwpractisephpphpcookbookerror	rack.php.php
      [line] => 7
      [function] => fun2
      [args] => Array
        (
        )
    )

	[3] => Array
    (
      [file] => D:wampwwwpractisephpphpcookbookerror	rack.php.php
      [line] => 30
      [function] => fun1
      [args] => Array
        (
        )
    )
)

  

参考:

<PHP Cookbook>3'rd  

原文地址:https://www.cnblogs.com/dee0912/p/5474138.html