时间:2021-07-01 10:21:17 帮助过:29人阅读
推荐学习:《PHP教程》
PHP打印调用堆栈的三种方法如下:
方法1:使用debug_print_backtrace()函数打印调用堆栈。
例:
- <?php
- // 用于打印PHP调用堆栈的PHP程序
- //调用函数parent_func
- function child_func() {
- parent_func();
- }
- // 调用grandparent_func函数
- function parent_func() {
- grandparent_func();
- }
- // 函数的作用是:打印调用堆栈
- function grandparent_func() {
- debug_print_backtrace();
- }
- //主函数调用
- child_func();
- ?>
输出:
- #0 grandparent_func() called at [/home/905a3b4d90f10b30521fedcb56c99fba.php:12]
- #1 parent_func() called at [/home/905a3b4d90f10b30521fedcb56c99fba.php:7]
- #2 child_func() called at [/home/905a3b4d90f10b30521fedcb56c99fba.php:21]
方法2:使用debug_backtrace()函数打印调用堆栈。
例:
- <?php
- // 用于打印PHP调用堆栈的PHP程序
- //函数调用parent_func
- function child_func() {
- parent_func();
- }
- // 函数调用grandparent_func
- function parent_func() {
- grandparent_func();
- }
- // 函数的作用是:打印调用堆栈
- function grandparent_func() {
- var_dump(debug_backtrace());
- }
- // 主函数调用
- child_func();
- ?>
输出:
- array(3) {
- [0]=>
- array(4) {
- ["file"]=>
- string(42) "/home/2b81f040639170c49a6a58adb23d5154.php"
- ["line"]=>
- int(12)
- ["function"]=>
- string(16) "grandparent_func"
- ["args"]=>
- array(0) {
- }
- }
- [1]=>
- array(4) {
- ["file"]=>
- string(42) "/home/2b81f040639170c49a6a58adb23d5154.php"
- ["line"]=>
- int(7)
- ["function"]=>
- string(11) "parent_func"
- ["args"]=>
- array(0) {
- }
- }
- [2]=>
- array(4) {
- ["file"]=>
- string(42) "/home/2b81f040639170c49a6a58adb23d5154.php"
- ["line"]=>
- int(21)
- ["function"]=>
- string(10) "child_func"
- ["args"]=>
- array(0) {
- }
- }
- }
方法3: Exception类的getTraceAsString()成员函数返回一个调用堆栈。
例:
- <?php
- // 用于打印PHP调用堆栈的PHP程序
- //函数调用parent_func
- function child_func() {
- parent_func();
- }
- // 函数调用grandparent_func
- function parent_func() {
- grandparent_func();
- }
- // 函数的作用是:打印调用堆栈
- function grandparent_func() {
- $e = new Exception;
- var_dump($e->getTraceAsString());
- }
- // 主函数调用
- child_func();
- ?>
输出:
- string(207) "#0 /home/8d8303d43667a4915d43dab7d63de26d.php(12): grandparent_func()
- #1 /home/8d8303d43667a4915d43dab7d63de26d.php(7): parent_func()
- #2 /home/8d8303d43667a4915d43dab7d63de26d.php(22): child_func()
- #3 {main}"
本篇文章就是关于PHP怎么打印调用堆栈的三种方法介绍,希望对需要的朋友有所帮助!
以上就是PHP怎么打印调用堆栈的详细内容,更多请关注Gxl网其它相关文章!