Debugging Applications

PHP offers tools to debug applications with notices, warnings, errors and exceptions. The Exception class offers information such as the file, line, message, numeric code, backtrace etc. on where an error occurred. OOP frameworks like Phalcon mainly use this class to encapsulate this functionality and provide information back to the developer or user.

Despite being written in C, Phalcon executes methods in the PHP userland, providing the debug capability that any other application or framework written in PHP has.

Catching Exceptions

Throughout the tutorials and examples of the Phalcon documentation, there is a common element that is catching exceptions. This is a try/catch block:

<?phptry{// ... Some Phalcon/PHP code}catch(\Exception$e){}

Any exception thrown within the block is captured in the variable $e. A Phalcon\Exception extends the PHP Exception class and is used to understand whether the exception came from Phalcon or PHP itself.

All exceptions generated by PHP are based on the Exception class, and have at least the following elements:

As you can see from the above output the Phalcon’s classes and methods are displayed just like any other component, and even showing the parameters that were invoked in every call. The method Exception::getTrace provides additional information if needed.

Debug component

Phalcon provides a debug component that allows the developer to easily find errors produced in an application
created with the framework.

The following screencast explains how it works:

To enable it, add the following to your bootstrap:

<?php$debug=new\Phalcon\Debug();$debug->listen();

Any Try/Catch blocks must be removed or disabled to make this component work properly.

Reflection and Introspection

Any instance of a Phalcon class offers exactly the same behavior than a PHP normal one. It’s possible to use the Reflection API or simply print any object to show how is its internal state:

<?php$router=newPhalcon\Mvc\Router();print_r($router);

It’s easy to know the internal state of any object. The above example prints the following:

Using XDebug

XDebug is an amazing tool that complements the debugging of PHP applications. It is also a C extension for PHP, and you can use it together with Phalcon without additional configuration or side effects.

The following screencast shows a Xdebug session with Phalcon:

Once you have xdebug installed, you can use its API to get a more detailed information about exceptions and messages.

We highly recommend using the latest version of XDebug for a better compatibility with Phalcon

<?phpusePhalcon\Mvc\Controller;classSignupControllerextendsController{publicfunctionindexAction(){}publicfunctionregisterAction(){// Request variables from HTML form$name=$this->request->getPost('name','string');$email=$this->request->getPost('email','email');// Stop execution and show a backtracereturnxdebug_print_function_stack('stop here!');$user=newUsers();$user->name=$name;$user->email=$email;// Store and check for errors$user->save();}}

In this instance, Xdebug will also show us the variables in the local scope, and a backtrace as well: