简单 php 代码跟踪调试实现
debug_backtrace:生成回溯
debug_print_backtrace:打印回溯
1. debug_backtrace ($options = DEBUG_BACKTRACE_PROVIDE_OBJECT, $limit = 0) {}
$backtrace = debug_backtrace();var_dump($backtrace);
get_user($user_id); // 订单资料 $order_info = array( 'order_id' => $order_id, 'order_name' => 'my order', 'user_info' => $user_info, ); return $order_info; }}class User{ // 获取用户资料 function get_user($user_id){ // 获取用户讯息 $oMessage = new Message; $user_message = $oMessage->get_message($user_id); $user_info = array( 'user_id' => $user_id, 'name' => 'fdipzone', 'message' => $user_message ); return $user_info; }}class Message{ // 获取用户讯息 function get_message($user_id){ $message = array( array('id'=>1, 'title'=>'message1'), array('id'=>2, 'title'=>'message2'), ); // 加入跟踪调试 $backtrace = debug_backtrace(); var_dump($backtrace); return $message; }}// 获取用户订单资料$order_id = 1000000;$oOrder = new Order;$order_info = $oOrder->get_order($order_id);
结果:
2.打印回溯
debug_print_backtrace (int $options = 0,int $limit = 0) :
$options :此参数是以下选项的位掩码:$limit :此参数可用于限制打印的堆栈帧的数目。
debug_print_backtrace();
代码:
get_user($user_id); // 订单资料 $order_info = array( 'order_id' => $order_id, 'order_name' => 'my order', 'user_info' => $user_info, ); return $order_info; }}class User{ // 获取用户资料 function get_user($user_id){ // 获取用户讯息 $oMessage = new Message; $user_message = $oMessage->get_message($user_id); $user_info = array( 'user_id' => $user_id, 'name' => 'fdipzone', 'message' => $user_message ); return $user_info; }}class Message{ // 获取用户讯息 function get_message($user_id){ $message = array( array('id'=>1, 'title'=>'message1'), array('id'=>2, 'title'=>'message2'), ); // 加入跟踪调试 //$backtrace = debug_backtrace(); //var_dump($backtrace);//fixme or debug_print_backtrace(); return $message; }}// 获取用户订单资料$order_id = 1000000;$oOrder = new Order;$order_info = $oOrder->get_order($order_id);
结果: