• 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 HomeController;
    use OTDataDictionary;
    
    //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] => HomeControllerIndexController
                [object] => HomeControllerIndexController Object
                    (
                        [view:protected] => ThinkView Object
                            (
                                [tVar:protected] => Array
                                    (
                                    )
    
                                [theme:protected] => 
                            )
    
                        [config:protected] => Array
                            (
                            )
    
                    )
    
                [type] => ->
                [args] => Array
                    (
                    )
    
            )
    
        [1] => Array
            (
                [file] => D:phpStudyWWWwwwrootRuntimecommon~runtime.php
                [line] => 1
                [function] => invoke
                [class] => ReflectionMethod
                [object] => ReflectionMethod Object
                    (
                        [name] => index
                        [class] => HomeControllerIndexController
                    )
    
                [type] => ->
                [args] => Array
                    (
                        [0] => HomeControllerIndexController Object
                            (
                                [view:protected] => ThinkView Object
                                    (
                                        [tVar:protected] => Array
                                            (
                                            )
    
                                        [theme:protected] => 
                                    )
    
                                [config:protected] => Array
                                    (
                                    )
    
                            )
    
                    )
    
            )
    
        [2] => Array
            (
                [file] => D:phpStudyWWWwwwrootRuntimecommon~runtime.php
                [line] => 1
                [function] => exec
                [class] => ThinkApp
                [type] => ::
                [args] => Array
                    (
                    )
    
            )
    
        [3] => Array
            (
                [file] => D:phpStudyWWWwwwrootThinkPHPLibraryThinkThink.class.php
                [line] => 117           //117行
                [function] => run     //Think.class.php文件的run方法
                [class] => ThinkApp
                [type] => ::
                [args] => Array
                    (
                    )
    
            )
    
        [4] => Array
            (
                [file] => D:phpStudyWWWwwwrootThinkPHPThinkPHP.php
                [line] => 94            //ThinkPHP文件的94行
                [function] => start   //经过了start函数
                [class] => ThinkThink
                [type] => ::
                [args] => Array
                    (
                    )
    
            )
    
        [5] => Array
            (
                [file] => D:phpStudyWWWwwwrootindex.php
                [line] => 39    //第39行
                [args] => Array
                    (
                        [0] => D:phpStudyWWWwwwrootThinkPHPThinkPHP.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 HomeController;
    use OTDataDictionary;
    
    //index 控制器
    class IndexController extends HomeController {
    
        //index方法
        public function index(){
            echo '<pre>';
            print_r(get_included_files());die;
       
                     
            $this->display();
        }
    
    }

    打印结果

    Array
    (
        [0] => D:phpStudyWWWwwwrootindex.php
        [1] => D:phpStudyWWWwwwrootThinkPHPThinkPHP.php
        [2] => D:phpStudyWWWwwwrootThinkPHPLibraryThinkThink.class.php
        [3] => D:phpStudyWWWwwwrootThinkPHPLibraryThinkStorage.class.php
        [4] => D:phpStudyWWWwwwrootThinkPHPLibraryThinkStorageDriverFile.class.php
        [5] => D:phpStudyWWWwwwrootRuntimecommon~runtime.php
        [6] => D:phpStudyWWWwwwrootApplicationCommonBehaviorInitHookBehavior.class.php
        [7] => D:phpStudyWWWwwwrootThinkPHPLibraryThinkBehavior.class.php
        [8] => D:phpStudyWWWwwwrootThinkPHPLibraryThinkCache.class.php
        [9] => D:phpStudyWWWwwwrootThinkPHPLibraryThinkCacheDriverFile.class.php
        [10] => D:phpStudyWWWwwwrootApplicationHomeConfconfig.php
        [11] => D:phpStudyWWWwwwrootApplicationHomeCommonfunction.php
        [12] => D:phpStudyWWWwwwrootThinkPHPLibraryBehaviorReadHtmlCacheBehavior.class.php
        [13] => D:phpStudyWWWwwwrootApplicationHomeControllerIndexController.class.php
        [14] => D:phpStudyWWWwwwrootApplicationHomeControllerHomeController.class.php
        [15] => D:phpStudyWWWwwwrootApplicationCommonApiConfigApi.class.php
        [16] => D:phpStudyWWWwwwrootThinkPHPLibraryThinkModel.class.php
        [17] => D:phpStudyWWWwwwrootThinkPHPLibraryThinkDb.class.php
        [18] => D:phpStudyWWWwwwrootThinkPHPLibraryThinkDbDriverMysqli.class.php
    )
  • 相关阅读:
    Linux IO接口 监控 (iostat)
    linux 防火墙 命令
    _CommandPtr 添加参数 0xC0000005: Access violation writing location 0xcccccccc 错误
    Visual Studio自动关闭
    Linux vsftpd 安装 配置
    linux 挂载外部存储设备 (mount)
    myeclipse 9.0 激活 for win7 redhat mac 亲测
    英文操作系统 Myeclipse Console 乱码问题
    Linux 基本操作命令
    linux 查看系统相关 命令
  • 原文地址:https://www.cnblogs.com/wt645631686/p/8124646.html
Copyright © 2020-2023  润新知