php的内置函数debug_backtrace()与get_included_files()跟踪代码调用(Thinkphp框架举例)

debug_backtrace()

在我们开发一个项目中,或者二开研究某个开源程序,需要对代码流程一步步去跟踪,来研究它的逻辑,才可以进行修改,达到我们的开发目的。php的内置函数debug_backtrace就具备这个功能,很直观的展示出从系统流程开始到执行终止的位置之前所走过的所有文件,函数,甚至调用的参数,还会具体到某个行数。

这里是官方的说明

http://php.net/manual/zh/function.debug-backtrace.php

下面我来用Thinkphp框架来举例,说明从进入index控制器下的index方法过程中,是怎么走过来的。

复制代码
<?php

namespace Home\Controller;
use OT\DataDictionary;

//index 控制器
class IndexController extends HomeController {

    //index方法
    public function index(){
        echo '<pre>';
        print_r(debug_backtrace());die;   //函数位置
   
                 
        $this->display();
    }

}
复制代码

然后执行PHP程序,看结果(从下往上看,才是执行流程)

复制代码
Array
(
    [0] => Array
        (
            [function] => index
            [class] => Home\Controller\IndexController
            [object] => Home\Controller\IndexController Object
                (
                    [view:protected] => Think\View Object
                        (
                            [tVar:protected] => Array
                                (
                                )

                            [theme:protected] => 
                        )

                    [config:protected] => Array
                        (
                        )

                )

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

        )

    [1] => Array
        (
            [file] => D:\phpStudy\WWW\wwwroot\Runtime\common~runtime.php
            [line] => 1
            [function] => invoke
            [class] => ReflectionMethod
            [object] => ReflectionMethod Object
                (
                    [name] => index
                    [class] => Home\Controller\IndexController
                )

            [type] => ->
            [args] => Array
                (
                    [0] => Home\Controller\IndexController Object
                        (
                            [view:protected] => Think\View Object
                                (
                                    [tVar:protected] => Array
                                        (
                                        )

                                    [theme:protected] => 
                                )

                            [config:protected] => Array
                                (
                                )

                        )

                )

        )

    [2] => Array
        (
            [file] => D:\phpStudy\WWW\wwwroot\Runtime\common~runtime.php
            [line] => 1
            [function] => exec
            [class] => Think\App
            [type] => ::
            [args] => Array
                (
                )

        )

    [3] => Array
        (
            [file] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Think.class.php
            [line] => 117           //117行
            [function] => run     //Think.class.php文件的run方法
            [class] => Think\App
            [type] => ::
            [args] => Array
                (
                )

        )

    [4] => Array
        (
            [file] => D:\phpStudy\WWW\wwwroot\ThinkPHP\ThinkPHP.php
            [line] => 94            //ThinkPHP文件的94行
            [function] => start   //经过了start函数
            [class] => Think\Think
            [type] => ::
            [args] => Array
                (
                )

        )

    [5] => Array
        (
            [file] => D:\phpStudy\WWW\wwwroot\index.php
            [line] => 39    //第39行
            [args] => Array
                (
                    [0] => D:\phpStudy\WWW\wwwroot\ThinkPHP\ThinkPHP.php
                )

            [function] => require
        )

)
复制代码

说明一下ThinkPHP框架的执行流程:

Index.php加载了Thinkphp.php文件 ---->  ThinkPHP.php执行Think.start() ----> Think.class.php中执行App::run() ----> App.class.php中执行 App::exec() ----->App::exec()中用反射方法调用了控制器

get_included_files()

此函数是打印在项目流程执行过程中被引入的文件

复制代码
<?php

namespace Home\Controller;
use OT\DataDictionary;

//index 控制器
class IndexController extends HomeController {

    //index方法
    public function index(){
        echo '<pre>';
        print_r(get_included_files());die;
   
                 
        $this->display();
    }

}
复制代码

打印结果

复制代码
Array
(
    [0] => D:\phpStudy\WWW\wwwroot\index.php
    [1] => D:\phpStudy\WWW\wwwroot\ThinkPHP\ThinkPHP.php
    [2] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Think.class.php
    [3] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Storage.class.php
    [4] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Storage\Driver\File.class.php
    [5] => D:\phpStudy\WWW\wwwroot\Runtime\common~runtime.php
    [6] => D:\phpStudy\WWW\wwwroot\Application\Common\Behavior\InitHookBehavior.class.php
    [7] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Behavior.class.php
    [8] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Cache.class.php
    [9] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Cache\Driver\File.class.php
    [10] => D:\phpStudy\WWW\wwwroot\Application\Home\Conf\config.php
    [11] => D:\phpStudy\WWW\wwwroot\Application\Home\Common\function.php
    [12] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Behavior\ReadHtmlCacheBehavior.class.php
    [13] => D:\phpStudy\WWW\wwwroot\Application\Home\Controller\IndexController.class.php
    [14] => D:\phpStudy\WWW\wwwroot\Application\Home\Controller\HomeController.class.php
    [15] => D:\phpStudy\WWW\wwwroot\Application\Common\Api\ConfigApi.class.php
    [16] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Model.class.php
    [17] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Db.class.php
    [18] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Db\Driver\Mysqli.class.php
)
复制代码

 

posted @   温柔的风  阅读(925)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示