PhpStorm 中 Xdebug 的条件断点

Conditional breakpoints for Xdebug in PhpStorm

假设我们有这两种方法:

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 会适得其反。

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

  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函数,并在断点处添加一个条件,只有在$superhero的值时才停止执行是 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";
        }
    }

像往常一样将断点放在 PhpStorm 中,然后 right-click 在编辑器栏中标记断点的红色圆盘上。在打开的弹出窗口 window 中输入您希望断点停止脚本执行的条件。可以在此处输入在您放置断点的代码中有效的任何条件。

例如输入$superhero===CHUCK_NORRIS.

按下 "Done" 按钮,您就可以开始了。像往常一样调试脚本。调试器会在每次命中断点时评估条件,但仅当条件评估为 true.

时才会停止脚本