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
(
[] => 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
(
) ) [] => Array
(
[file] => D:\phpStudy\WWW\wwwroot\Runtime\common~runtime.php
[line] =>
[function] => invoke
[class] => ReflectionMethod
[object] => ReflectionMethod Object
(
[name] => index
[class] => Home\Controller\IndexController
) [type] => ->
[args] => Array
(
[] => Home\Controller\IndexController Object
(
[view:protected] => Think\View Object
(
[tVar:protected] => Array
(
) [theme:protected] =>
) [config:protected] => Array
(
) ) ) ) [] => Array
(
[file] => D:\phpStudy\WWW\wwwroot\Runtime\common~runtime.php
[line] =>
[function] => exec
[class] => Think\App
[type] => ::
[args] => Array
(
) ) [] => Array
(
[file] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Think.class.php
[line] => //117行
[function] => run //Think.class.php文件的run方法
[class] => Think\App
[type] => ::
[args] => Array
(
) ) [] => Array
(
[file] => D:\phpStudy\WWW\wwwroot\ThinkPHP\ThinkPHP.php
[line] => //ThinkPHP文件的94行
[function] => start //经过了start函数
[class] => Think\Think
[type] => ::
[args] => Array
(
) ) [] => Array
(
[file] => D:\phpStudy\WWW\wwwroot\index.php
[line] => //第39行
[args] => Array
(
[] => 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
(
[] => D:\phpStudy\WWW\wwwroot\index.php
[] => D:\phpStudy\WWW\wwwroot\ThinkPHP\ThinkPHP.php
[] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Think.class.php
[] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Storage.class.php
[] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Storage\Driver\File.class.php
[] => D:\phpStudy\WWW\wwwroot\Runtime\common~runtime.php
[] => D:\phpStudy\WWW\wwwroot\Application\Common\Behavior\InitHookBehavior.class.php
[] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Behavior.class.php
[] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Cache.class.php
[] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Cache\Driver\File.class.php
[] => D:\phpStudy\WWW\wwwroot\Application\Home\Conf\config.php
[] => D:\phpStudy\WWW\wwwroot\Application\Home\Common\function.php
[] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Behavior\ReadHtmlCacheBehavior.class.php
[] => D:\phpStudy\WWW\wwwroot\Application\Home\Controller\IndexController.class.php
[] => D:\phpStudy\WWW\wwwroot\Application\Home\Controller\HomeController.class.php
[] => D:\phpStudy\WWW\wwwroot\Application\Common\Api\ConfigApi.class.php
[] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Model.class.php
[] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Db.class.php
[] => D:\phpStudy\WWW\wwwroot\ThinkPHP\Library\Think\Db\Driver\Mysqli.class.php
)

最新文章

  1. 数据库的Disk Space usage
  2. SOA架构介绍和理解
  3. jquery获取和设置元素高度宽度
  4. 使用before、after伪类制作三角形
  5. 织梦cms PHPcms 帝国cms比较
  6. oracle误删除恢复
  7. Linq中延迟查询和立即查询
  8. Java比较运算符
  9. C# Winform对文件夹的权限判断及处理
  10. WINCE平台下C#应用程序中使用看门狗
  11. Python 文本解析器
  12. Visual Studio GitHub For Windows部署
  13. WCF服务承载
  14. LPC1788定时器使用
  15. JAVA面向对象-----main方法详解
  16. Tomcat相关面试题,看这篇就够了!保证能让面试官颤抖!
  17. [zz] MATLAB工具箱介绍
  18. 关于SQL Server将一列的多行内容拼接成一行,合并显示在另外表中
  19. 论文笔记之 SST: Single-Stream Temporal Action Proposals
  20. x1c 2018 静音调教

热门文章

  1. 免费内网映射外网绑定,tcp端口转发(windows)
  2. 图表工具--- ECharts.js学习(一) 简单入门
  3. js swipeDelete 滑动删除
  4. centos7创建新用户
  5. commons-logging的使用
  6. Struts2.3.34+Hibernate 4.x+Spring4.x 整合二部曲之上部曲
  7. OpenFlow交换机的实现总结
  8. openvpn的搭建
  9. docker之NGINX镜像构建
  10. 自己做的notepad++ FTP同步插件