xdebug 调试_使用Xdebug和Sublime Text 3进行调试

xdebug 调试

Debugging – we all do it a lot. Writing code perfectly the first time around is hard and only a few (if any) succeed at it. More than a year ago, Shameer wrote an article on SitePoint about how you can debug your application using Xdebug and Netbeans. In this article, we are going to have a look at how we can debug using Xdebug in combination with Sublime Text.

调试–我们都做了很多。 第一次很难完美地编写代码,只有很少(如果有的话)成功。 一年多以前,Shameer在SitePoint上写了一篇文章 ,介绍如何使用Xdebug和Netbeans调试应用程序。 在本文中,我们将了解如何结合使用Xdebug和Sublime Text进行调试。

入门 (Getting started)

First of all, we need to have the PHP Xdebug extension installed. If you are uncertain on how to get this done, please have a look at the link provided in the introduction. Make sure that Xdebug is working by checking if it’s listed in your phpinfo(). Of course we also need Sublime Text. I will be using the latest version: Sublime Text 3. It should also work with Sublime Text 2.

首先,我们需要安装PHP Xdebug扩展。 如果您不确定如何完成此操作,请查看简介中提供的链接。 通过检查phpinfo()是否列出了Xdebug,以确保其正常工作。 当然,我们还需要Sublime Text。 我将使用最新版本:Sublime Text3。它也应该与Sublime Text 2一起使用。

设置Xdebug (Setting up Xdebug)

We need to configure xdebug by adding the following to your php.ini file, or even better, to an xdebug.ini file as described here under How-to On Linux.

我们通过添加以下到您需要配置XDebug的php.ini文件,甚至更好,一个xdebug.ini描述文件在这里下的操作方法在Linux上。

xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000
xdebug.remote_log="/var/log/xdebug/xdebug.log"

In general you will be using 127.0.0.1 as your host. However, If you are using vagrant for example, you will be using something like 10.0.2.2, depending on where Xdebug can find your system.

通常,您将使用127.0.0.1作为主机。 但是,例如,如果您使用的是无业游民,则将使用10.0.2.2之类的东西,具体取决于Xdebug在哪里可以找到您的系统。

The remote log is not necessary, but in case of problems, it’s the place where you can find information about errors that occurred.

远程日志不是必需的,但是如果出现问题,可以在这里找到有关发生的错误的信息。

Don’t forget to restart your webserver!

不要忘记重启您的网络服务器!

设置Sublime Text 3 (Setting up Sublime Text 3)

One of the strengths of Sublime is the fact that you can extend it easily with packages. In this case, we are going to install the Xdebug package. If you haven’t done so already, make sure you can install packages by installing package control.

Sublime的优势之一是您可以轻松地使用软件包对其进行扩展。 在这种情况下,我们将安装Xdebug软件包。 如果尚未安装软件包控制 ,请确保可以安装软件包。

alt

Once you have the package control installed, you should start Sublime Text 3. Open up the command palette from the tools menu and search for “install package”.

一旦安装了软件包控件,就应该启动Sublime Text3。从工具菜单中打开命令面板,然后搜索“ install package”。

alt

Now you can search for any package you like. In our case, we are going to search for the package “Xdebug client”.

现在,您可以搜索所需的任何软件包。 在本例中,我们将搜索“ Xdebug客户端”包。

The last bit we have to do is set up the project within Sublime. The easiest way to do this is to open up the root directory of your application, go to projects and click on “save projects as”. I suggest you save the file within the root of your application, so you can save it in your version control system if you are using any and you can configure it easily at all times.

我们要做的最后一点是在Sublime中设置项目。 最简单的方法是打开应用程序的根目录,转到项目,然后单击“将项目另存为”。 我建议您将文件保存在应用程序的根目录中,以便在使用任何文件时都可以将其保存在版本控制系统中,并且可以随时对其进行轻松配置。

Open up the just created project file. The content will look like this:

打开刚刚创建的项目文件。 内容将如下所示:

{
    "folders":
    [
        {
           "follow_symlinks": true,
           "path": "."
        }
    ]
}

We are going to add a few more lines:

我们将添加更多行:

{
    "folders":
    [
        {
            "follow_symlinks": true,
            "path": "."
        }
    ],
    "settings": {
        "xdebug": {
             "url": "http://my.local.website/",
        }
    }
}

As you can see, I only added a URL to my actual web application. I could set more settings for Xdebug, however, this is enough to start with. I could have also set this URL in the Xdebug settings itself, but in that case, I couldn’t work on multiple projects without having to change the Xdebug config each time.

如您所见,我仅在实际的Web应用程序中添加了URL。 我可以为Xdebug设置更多设置,但是,这足以开始。 我也可以在Xdebug设置本身中设置此URL,但是在那种情况下,我不能在不必每次更改Xdebug配置的情况下处理多个项目。

启动Xdebug会话 (Start the Xdebug session)

We can now start the Xdebug session to see if everything is set up properly. In the menu, click on tools -> Xdebug and click on start debugging (launch browser). You will notice that your website is opened up and that ?XDEBUG_SESSION_START=sublime.xdebug is added to the end of the URL. This will start the xdebug session. In Sublime, some extra panels appear where debug information will be shown, after you have set one or more breakpoints.

现在,我们可以启动Xdebug会话,以查看是否一切设置正确。 在菜单中,单击工具-> Xdebug,然后单击开始调试(启动浏览器)。 您会注意到您的网站已打开,并且在URL的末尾添加了?XDEBUG_SESSION_START=sublime.xdebug 。 这将启动xdebug会话。 在设置了一个或多个断点之后,在Sublime中,将显示一些其他面板,其中将显示调试信息。

断点 (Breakpoints)

Let’s set out first breakpoint. A breakpoint is basically a flag where your application will halt when it reaches it. At the moment it halts, you can inspect all the variables’ values so you know actually what is going on.

让我们列出第一个断点。 断点基本上是一个标志,您的应用程序在到达该位置时会中断。 在它停止的那一刻,您可以检查所有变量的值,以便知道实际情况。

We can add a breakpoint by clicking with our right mouse on a line, going to Xdebug and then clicking on add/remove breakpoint. A marker will be added to the line gutter to indicate that a breakpoint has been set.

我们可以通过以下方式添加断点:用鼠标右键单击一行,转到Xdebug,然后单击“添加/删除断点”。 标记将添加到线槽中,以指示已设置断点。

alt

We open up our browser again and continue with the session we just started. You will notice that as soon as you go to the page where the breakpoint is, the page will stop loading. If you now open up Sublime, you will see a lot of information shown in the Xdebug panels.

我们再次打开浏览器,然后继续我们刚刚开始的会话。 您会注意到,一旦转到断点所在的页面,该页面就会停止加载。 如果现在打开Sublime,您将在Xdebug面板中看到很多信息。

The Xdebug stack and Xdebug context are very interesting. In the stack, you can see the whole stacktrace your call went through.

Xdebug堆栈和Xdebug上下文非常有趣。 在堆栈中,您可以看到调用经过的整个堆栈跟踪。

In the context, you will see all global variables, but also the variables you defined yourself. You can click on these variables to see exactly these variables are holding. For instance, in the screenshot below, I clicked on the $_SERVER variable.

在上下文中,您将看到所有全局变量,以及您自己定义的变量。 您可以单击这些变量以查看这些变量是否正确。 例如,在下面的屏幕快照中,我单击了$ _SERVER变量。

alt

Notice that a yellow arrow is pointing at the line the application is currently halted on.

请注意,黄色箭头指向应用程序当前暂停所在的行。

So our application halted and now we can look through the variables defined. However, we are done and we want to move on. What now? When you right mouse click once again and hover over the Xdebug menu, you will have several options:

因此,我们的应用程序停止了,现在我们可以浏览定义的变量。 但是,我们已经完成了,我们想继续前进。 现在怎么办? 再次右键单击并悬停在Xdebug菜单上时,您将有几个选项:

  • Run Which will run the application until the next breakpoint or until the ending.

    运行它将运行应用程序,直到下一个断点或结束为止。

  • Run to line which will run until the line you clicked.

    运行到将一直运行到您单击的行的行。

  • Step into will step into the current function and stops right after.

    进入将进入当前功能,然后立即停止。

  • Step over Will step over the current function and stops right after.

    单步执行将单步执行当前功能,然后立即停止。

  • Step out Will step out of the current function and stop right after.

    退出将退出当前功能,然后立即停止。

  • Stop Will stop debugging.

    停止将停止调试。

  • Detach Will also stop debugging.

    分离还将停止调试。

Run and stop are quite easy to understand. The step methods could be a little confusing. Let’s dive into these with a simple example.

运行和停止非常容易理解。 步骤方法可能会有些混乱。 让我们通过一个简单的例子来深入研究这些。

Class Foo() 
{

    public function bar(Array $arr)
    {
        $arr = self::fooBar($arr); // Breakpoint
        return $arr;
    }

    public function fooBar(Array $arr)
    {
        return array_values($arr);
    }
}

Imagine you added a breakpoint to the first line of the method bar. So on the line with the breakpoint comment (// breakpoint).

假设您在方法栏的第一行添加了一个断点。 因此,在带有断点注释( // breakpoint )的行上。

With step into, the debugger will step into the fooBar method and will stop there at the first line. So in this case, the debugger will halt on the return array_values($arr); line.

进入后,调试器将进入fooBar方法,并在第一行停止。 因此,在这种情况下,调试器将停止return array_values($arr); 线。

Step over will call the method, but will not stop. It will stop at the next line available after calling the method. So in this case, it will stop at return $arr;

单步执行将调用该方法,但不会停止。 调用该方法后,它将在下一行可用处停止。 因此,在这种情况下,它将在return $arr;处停止return $arr;

Lastly, with step out it will run through the whole bar method and return to the caller. In this case, it will go out of the object, back to the original caller.

最后,退出时,它将贯穿整个bar方法并返回到调用方。 在这种情况下,它将离开对象,回到原始调用者。

If you just decide to run, the application will run further until the moment it is done executing or another breakpoint occurs.

如果您决定运行,则应用程序将继续运行,直到执行完成或出现另一个断点为止。

结论 (Conclusion)

In this article we saw how we could integrate Xdebug with Sublime and made sure we understood how to debug. Almost every IDE suitable for PHP can integrate with Xdebug. If you are interested in debugging like this in Netbeans, have a look at the article mentioned in the introduction. Are you using breakpoints? Or are you using PHP functions like var_dump to get your debug data? Let us know in the comments below!

在本文中,我们了解了如何将Xdebug与Sublime集成在一起,并确保我们了解如何调试。 几乎所有适用于PHP的IDE都可以与Xdebug集成。 如果您有兴趣在Netbeans中进行此类调试,请查看引言中提到的文章。 您在使用断点吗? 还是使用诸如var_dump之类PHP函数来获取调试数据? 在下面的评论中让我们知道!

翻译自: https://www.sitepoint.com/debugging-xdebug-sublime-text-3/

xdebug 调试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值