为 PhpStorm 配置 Xdebug 来调试代码

为 PhpStorm 配置 Xdebug 来调试代码

当项目越来越复杂,排错就越发困难。
你以为代码是这么运行的,但就是有未想到的功能导致流程变得不可捉摸。
此时我们需要调试啊调试...

PhpStorm 是一款优秀的 PHP IDE,排除其 Java 系出身导致的资源占用情况不理想外,其功能和易用性是毋庸质疑的。
好,再说下去就是软文了。

PhpStorm 内建了 Zend Debugger 和 Xdebug 支持,使用简单的配置我们就可以开始调试代码了。我们以 Xdebug 为例来说明下。

为 PHP 安装 Xdebug 扩展

Win 下的安装较为简单,直接下载适合的版本的 dll 文件,写入到 php.ini 配置中就可以了。
我们介绍下源代码编译方式。

Xdebug 下载页面 找到最新的源代码,现在的版本是 2.2.1

cd /Users/micate/Downloads/wget http://xdebug.org/files/xdebug-2.2.1.tgztar zxvf xdebug-2.2.1.tgzcd xdebug-2.2.1/
 
phpize
.
/configure; makesudo make install

嗯,不出意外的话,会提示 xdebug.so 已经被放置在 ... 位置上。记住这个位置,复制下来。

打开 php.ini,在最后面追加下述配置:

[xdebug]zend_extension="/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
 
xdebug.idekey
="PHPSTORM"
 
xdebug.remote_host
=127.0.0.1
 
xdebug.remote_enable
=on

上面的位置要与刚刚安装的位置保持一致。关于 xdebug.so 如何配置,Xdebug 文档中有说明

Configure PHP to Use Xdebug

  1. add the following line to php.ini: zend_extension="/wherever/you/put/it/xdebug.so" (for non-threaded use of PHP, for example the CLI, CGI or Apache 1.3 module) or: zend_extension_ts="/wherever/you/put/it/xdebug.so" (for threaded usage of PHP, for example the Apache 2 work MPM or the the ISAPI module). Note: In case you compiled PHP yourself and used --enable-debug you would have to use zend_extension_debug=. From PHP 5.3 onwards, you always need to use the zend_extension PHP.ini setting name, and not zend_extension_ts, nor zend_extension_debug. However, your compile options (ZTS/normal build; debug/non-debug) still need to match with what PHP is using.
  2. Restart your webserver.
  3. Write a PHP page that calls 'phpinfo()' Load it in a browser and look for the info on the Xdebug module. If you see it next to the Zend logo, you have been successful! You can also use 'php -m' if you have a command line version of PHP, it lists all loaded modules. Xdebug should appear twice there (once under 'PHP Modules' and once under 'Zend Modules').

其中使用 zend_extension_ts 还是 zend_extension 还是神马神马,与 PHP 版本有关,仔细看上面的说明。

重启下 Web Server(Apache / Nginx),使用 phpinfo() 页面或命令行工具,确认模块安装完成。

配置 PhpStorm

本节参考了 Configuring PHP debugging in PhpStorm 2.0 一文:

PhpStorm 以最新版的 4.0.3 为例,版本不同操作会有些许区别。

打开 PhpStorm,新建或打开一个项目,打开偏好设置界面:

依图所示,确认 Debug 设置无误(提示,可以在左上角直接搜索 Debug 来定位到设置项):

单击左侧的 Servers,添加一个 Debug Server:

其中 Host 和 Port 根据自己要调试的实际情况来写,先不要加目录或查询字符串。后面选择 Xdebug。
然后 Apply 和 OK,回到编辑器界面。

点击菜单里的 Run - Configurations:

在 运行/调试 对话框中,依图配置:

如图所示:点击 添加 按钮,选择 PHP Web Application,并在右侧输入配置名称,选择我们刚刚添加的 Server,Start URL 中填写调试可能所需的查询字符串,选择好默认浏览器,最后点击 Apply 和 OK,确认配置。

然后,还有一步:

  1. 在程序中设置好断点;
  2. 在工具栏中,选择好要调试的应用;
  3. 点击 Start Listen PHP Debug Connections 按钮,即那个红色的电话按钮,让其变成绿色,即开始监听 PHP Debug 连接;

于是,终于可以开始了。
点击上图中的 Debug 按钮,或者从菜单中选择 Run - Debug,你就开始了 Debug 之旅...

其他说明

根据断点配置,或在打开 Debug URL 的过程中,或在 POST 之后,如果 PhpStorm 监听到了 Debug 连接,就会立即切换到编辑器界面,并跳转到设置的断点处,浏览器端会等待 PhpStorm 的操作。

你可以随时中断 PhpStorm 的调试,或方便的进行 Step Into / Step Over / Run to cursor(这个刁爆了):

熟悉 Java 的童鞋会对这个界面感到亲切,吼吼。

哎呀,不想调试了,PhpStorm 却总是跳出来?记得刚刚那个电话按钮嘛,再点一下,让其变成红色,就好了。

先到这里,嗯哼。自己捣鼓捣鼓吧。

 
原文地址:https://www.cnblogs.com/holyes/p/3241170.html