代码之家  ›  专栏  ›  技术社区  ›  Dimitrios Desyllas

PhpStorm中Xdebug的条件断点

  •  5
  • Dimitrios Desyllas  · 技术社区  · 7 年前

    假设我们有以下两种方法:

    function superFunction($superhero,array $clothes){
        if($superhero===CHUCK_NORRIS){
          wear($clothes);
        } else if($superhero===SUPERMAN) {
          wear($clothes[1]);
        }
    }
    
    function wear(array $clothes)
    {
       for($piece in $clothes){
           echo "Wearing piece";
       }
    }
    

    因此,我想要实现的是在PhpStorm中将断点放入函数中 wear 但我只想在 $superhero 变量具有值 CHUCK_NORRIS 我怎么能做到这一点。假设函数 superFunction 被传唤了无数次 F9层 一直以来都是适得其反的。

    2 回复  |  直到 7 年前
        1
  •  6
  •   axiac    7 年前

    像往常一样将断点放在PhpStorm中,然后右键单击编辑器槽中标记断点的红色光盘。在打开的弹出窗口中,输入希望断点停止脚本执行的条件。可以在此处输入在放置断点的代码中有效的任何条件。

    例如,输入 $superhero===CHUCK_NORRIS

    按“完成”按钮,就可以开始了。像往常一样调试脚本。每次命中断点时,调试器都会计算条件,但只有当条件计算为时,调试器才会停止脚本 true

        2
  •  3
  •   Mihai Matei    7 年前

    正如我已经评论过的,至少有两种可能的方法可以实现这一点:

    1. 将断点放在函数调用上(在if语句中),然后单步执行函数

      function superFunction($superhero, array $clothes)
      {
          if ($superhero === CHUCK_NORRIS){
              wear($clothes); // <---- put the break point on this line
          } elseif ($superhero === SUPERMAN) {
              wear($clothes[1]);
          }
      }
      
    2. 通过 $superhero 值作为参数 wear 函数并在断点上添加一个条件,仅当 $超级英雄 的值为 CHUCK_NORRIS

    进入函数

        function superFunction($superhero,array $clothes)
        {
            if ($superhero === CHUCK_NORRIS) {
                wear($clothes, $superhero); // <---- passing the $superhero variable
            } elseif ($superhero === SUPERMAN) {
                wear($clothes[1]);
            }
        }
    
        function wear(array $clothes, $superhero = null)
        {
            for ($piece in $clothes) { // <---- conditional break point here: $superhero === CHUCK_NORRIS
                echo "Wearing piece";
            }
        }