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 )