PHP 调试追踪debug_backtrace()函数

PHP 调试追踪技巧

作用

使用调试追踪函数对应开发框架或调试错误有很大帮助,使用调试追踪函数可以编写类型tp框架中的trace()函数,打印除漂亮调试样式。

以下与个人学习开发的框架(类似tp框架)的Index控制中的index方法中进行打印调试测试

debug_backtrace()

  • PHP系统函数,可以追踪当前代码执行过程
  • 输出数据说明
名称类型描述
functionstring当前的函数名。
lineinteger当前的行号。
filestring当前的文件名。
classstring当前的类名。
objectobject当前对象。
typestring当前的调用类型,可能的调用:返回:"->" - 方法调用返回:"::" - 静态方法调用返回 nothing - 函数调用
argsarray如果在函数中,列出函数参数。如果在被引用的文件中,列出被引用的文件名。
  • 代码演示
class IndexController extends Controller{
    public function index(){
        $trace = debug_backtrace();
        dump($trace);
    }
}

?>
  • 输出结果
Array
(
    [0] => Array
        (
            [file] => E:\rufeike\xdqphp\XDQPHP\Lib\Core\Application.class.php
            [line] => 29
            [function] => index
            [class] => IndexController
            [object] => IndexController Object
                (
                )

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

        )

    [1] => Array
        (
            [file] => E:\rufeike\xdqphp\XDQPHP\Lib\Core\Application.class.php
            [line] => 17
            [function] => _app_run
            [class] => Application
            [type] => ::
            [args] => Array
                (
                )

        )

    [2] => Array
        (
            [file] => E:\rufeike\xdqphp\XDQPHP\XDQPHP.php
            [line] => 20
            [function] => run
            [class] => Application
            [type] => ::
            [args] => Array
                (
                )

        )

    [3] => Array
        (
            [file] => E:\rufeike\xdqphp\XDQPHP\XDQPHP.php
            [line] => 96
            [function] => run
            [class] => XDQPHP
            [type] => ::
            [args] => Array
                (
                )

        )

    [4] => Array
        (
            [file] => E:\rufeike\xdqphp\index.php
            [line] => 11
            [args] => Array
                (
                    [0] => E:\rufeike\xdqphp\XDQPHP\XDQPHP.php
                )

            [function] => require_once
        )

)

debug_print_backtrace()

  • 页面直接调用该函数,可以在页面直接输出当前代码执行流程追踪信息
<?php
class IndexController extends Controller{
    public function index(){
        debug_print_backtrace();//直接调用打印输出
      
    }
}

?>
  • 输出结果
#0  IndexController->index() called at [E:\rufeike\xdqphp\XDQPHP\Lib\Core\Application.class.php:29]
#1  Application::_app_run() called at [E:\rufeike\xdqphp\XDQPHP\Lib\Core\Application.class.php:17]
#2  Application::run() called at [E:\rufeike\xdqphp\XDQPHP\XDQPHP.php:20]
#3  XDQPHP::run() called at [E:\rufeike\xdqphp\XDQPHP\XDQPHP.php:96]
#4  require_once(E:\rufeike\xdqphp\XDQPHP\XDQPHP.php) called at [E:\rufeike\xdqphp\index.php:11]
  • 可以开启缓冲区,进行最终信息获取
    • 第一步:开启缓存区 ob_start();
    • 第二步:缓冲区中打印输出 debug_print_backtrace();
    • 第三步:从缓冲区中取出,放置到一个变量中 $var = ob_get_clean();
    • 注意:存在变量时,可以对缓存区的数据进行实体化,使用htmlspecialchars()
<?php
class IndexController extends Controller{
    public function index(){
        ob_start();//开启缓存区
        debug_print_backtrace();//缓冲区中输出打印
        $var = htmlspecialchars(ob_get_clean());//从缓存区中取值追踪信息
        dump($var);
    }
}

?>

输出结果

#0  IndexController->index() called at [E:\rufeike\xdqphp\XDQPHP\Lib\Core\Application.class.php:29]
#1  Application::_app_run() called at [E:\rufeike\xdqphp\XDQPHP\Lib\Core\Application.class.php:17]
#2  Application::run() called at [E:\rufeike\xdqphp\XDQPHP\XDQPHP.php:20]
#3  XDQPHP::run() called at [E:\rufeike\xdqphp\XDQPHP\XDQPHP.php:96]
#4  require_once(E:\rufeike\xdqphp\XDQPHP\XDQPHP.php) called at [E:\rufeike\xdqphp\index.php:11]
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值