dqqyp90576 2016-02-05 15:19
浏览 60
已采纳

debug_backtrace()捕获什么状态的函数参数?

Some abstract code:

function test($a = 5) {
  debug_backtrace();
  a = 10;
}

What will debug_trace tell us about arguments of the test function?
Will it capture $a as 5 or 10?

  • 写回答

1条回答 默认 最新

  • dqaq59269 2016-02-05 15:51
    关注

    If we call the function from the example this way:

    test(4);
    

    it'll capture '4'.

    And if we call it this way:

    test();
    

    it'll capture actually no data about the arguments. I suppose parser doesn't initialize arguments if they haven't been used anywhere. (Calling debug_backtrace doesn't count.)

    I've done some more researches and things have turned out a little bit unexpected (personally for me) if passing arguments by reference... But logical enough, I admit.

    If we use the following code:

    <?php
        function test2(&$a) {
                $a = 5;
                test($a);
                $a = 8;
        }
        function test(&$a) {
                $a = 6;
                print_r(debug_backtrace());
                $a = 7;
        }
        $test = 1;
        test2($test);
    

    We'll get such output:

    Array (
      [0] => Array (
        [file] => /var/www/localhost/htdocs/index.php
        [line] => 4
        [function] => test
        [args] => Array ( [0] => 6 ) 
      )
      [1] => Array (
        [file] => /var/www/localhost/htdocs/index.php
        [line] => 13
        [function] => test2
        [args] => Array ( [0] => 6 ) 
      ) 
    )
    

    So debug_backtrace() always prints current state of function arguments passed by references (when debug_backtrace() was actually called), no matter if they had another value on a parent function call.
    Be careful when debugging! :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持