php扩展xdebug基本使用

来源:互联网 发布:血族手游 知乎 编辑:程序博客网 时间:2024/04/30 22:00

使用

 

1.获取文件名,行号,函数名

xdebug_call_class()

<?php
    function fix_string($a)
    {
        echo "Called @ ".
            xdebug_call_file().
            ":".
            xdebug_call_line().
            " from ".
            xdebug_call_function();
    }

    $ret = fix_string(array('Derick'));
?>


输出:

Called @ /var/www/xdebug_caller.php:12 from {main}
 
2.输出head信息
xdebug_get_headers() 
[php] view plaincopy
  1. <?php  
  2. header( "X-Test""Testing" );  
  3. setcookie( "TestCookie""test-value" );  
  4. var_dump( xdebug_get_headers() );  
  5. ?>  


输出:

array(2) {  [0]=>  string(6) "X-Test"  [1]=>  string(33) "Set-Cookie: TestCookie=test-value"}

 

 

3.输出执行时间

xdebug_time_index()

[html] view plaincopy
  1. <?php  
  2. echo xdebug_time_index(), "\n";  
  3. for ($i = 0; $i < 250000; $i++)  
  4. {  
  5.     // do nothing  
  6. }  
  7. echo xdebug_time_index(), "\n";  
  8. ?>  

4.代码覆盖

xdebug_start_code_coverage();

代码

var_dump(xdebug_get_code_coverage());

看图更明显

5.代码跟踪

需要跟踪的代码:

[php] view plaincopy
  1. class myClass{  
  2.     public function a($a) {  
  3.         echo $a * 2.5;  
  4.     }  
  5.   
  6.     public function b($b) {  
  7.        $this->a($b + 2);  
  8.     }  
  9. }  
  10. xdebug_start_trace('trace');  
  11. $obj=new myClass();  
  12. $obj->b(6);  
  13. echo "</br>";  
  14. xdebug_stop_trace();  

--------------------------------------------------------------------------
xdebug配置参考

[php] view plaincopy
  1. xdebug.default_enable = On  
  2. xdebug.show_exception_trace = On  
  3. xdebug.show_local_vars = 1  
  4. xdebug.max_nesting_level = 50  
  5. xdebug.var_display_max_depth = 6  
  6.   
  7. xdebug.dump_once = On  
  8. xdebug.dump_globals = On  
  9. xdebug.dump_undefined = On  
  10. xdebug.dump.REQUEST = *  
  11. xdebug.dump.SERVER = REQUEST_METHOD,REQUEST_URI,HTTP_USER_AGENT  
  12.   
  13. xdebug.trace_format = 0  
  14. xdebug.auto_trace = On  
  15. xdebug.trace_output_dir = E:\xampp\tmp\traces  
  16. xdebug.trace_output_name = trace.%c.%p  
  17.   
  18. xdebug.collect_params = 4  
  19. xdebug.collect_includes = On  
  20. xdebug.collect_return = On  
  21. xdebug.show_mem_delta = On  

设置xdebug.auto_trace = Off可在代码中添加xdebug_start_trace();xdebug_stop_trace();语句生成追踪文件。


使用xdebug_start_trace跟踪代码执行

[php] view plaincopy
  1. xdebug_start_trace('trace');$a->myCaller($b);xdebug_stop_trace();TRACE START [16:53:57]0.0010      57964    -> MyClass->myCaller() /code/xdebug.php:210.0011      58104     -> MyOther->myCallee() /code/xdebug.php:40.0011      58104      -> xdebug_call_class() /code/xdebug.php:100.0011      58128      -> printf() /code/xdebug.php:100.0014      58196      -> xdebug_call_function() /code/xdebug.php:110.0015      58196      -> printf() /code/xdebug.php:110.0016      58196      -> xdebug_call_file() /code/xdebug.php:120.0016      58244      -> printf() /code/xdebug.php:120.0017      58244      -> xdebug_call_line() /code/xdebug.php:130.0017      58244      -> printf() /code/xdebug.php:130.0018      58244   -> xdebug_stop_trace() /code/xdebug.php:220.0019      58244TRACE END [16:53:57]  

0 0