7
php 的调试方法-查看调用堆栈
source link: https://nicksxs.me/2023/12/31/php-%E7%9A%84%E8%B0%83%E8%AF%95%E6%96%B9%E6%B3%95-%E6%9F%A5%E7%9C%8B%E8%B0%83%E7%94%A8%E5%A0%86%E6%A0%88/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
php 的调试方法-查看调用堆栈
php 代码调试里的神器就是 echo 111;exit;
但是对于使用了接口和继承比较多的话,有时候比较难找,可能定位到了一段代码但是不知道怎么调用过来的,这时候就可以用这个方法
总结下来有三种,
第一种是最巧妙的
function a() {
echo 111;exit;
}
比如本来是上面这样子,那么其实我们可以主动new 个异常
function a() {
$e = new Exception();
print_r($e->getTraceAsString());
echo 111;exit;
}
这样我的 trace 调用链路就出来了
这个就是比较简单的,调用 php 自身提供的方法
debug_backtrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit = 0): array
第一个参数是个掩码
debug_backtrace() | Populates both indexes |
---|---|
debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT) | |
debug_backtrace(1) | |
debug_backtrace(0) | Omits index "object" and populates index "args" . |
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) | Omits index "object" and index "args" . |
debug_backtrace(2) | |
debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS) |
debug_backtrace(3) |
第二个参数是限制栈深度
这个也是用自身的方法
debug_print_backtrace(int $options = 0, int $limit = 0): void
这里的第一个参数只有一个可以传的
就是隐藏参数,不然如果对于一些框架代码,这个打印会非常大,需要注意下
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK