PhpStorm 中 Xdebug 的条件断点

Dim*_*las 9 php 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";
   }
}
Run Code Online (Sandbox Code Playgroud)

所以我想要实现的是将 PhpStorm 中的断点放入函数中wear,但我想仅当$superhero变量具有我可以做到这一点的值时才被解雇CHUCK_NORRIS。想象一下,该函数superFunction被调用了无数次,并且F9一直按下去会适得其反。

axi*_*iac 9

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

例如,输入$superhero===CHUCK_NORRIS.

按“完成”按钮即可开始。像往常一样调试脚本。每次命中断点时,调试器都会评估条件,但仅当条件评估为 时才会停止脚本true


Mat*_*hai 3

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

  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]);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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";
        }
    }
Run Code Online (Sandbox Code Playgroud)