The Debug Component

The Debug Component

The Debug component provides tools to ease debugging PHP code.

Installation

You can install the component in many different ways:

Then, require the vendor/autoload.php file to enable the autoloading mechanism provided by Composer. Otherwise, your application won't be able to find the classes of this Symfony component.

Usage

The Debug component provides several tools to help you debug PHP code. Enabling them all is as easy as it can get:

use Symfony\Component\Debug\Debug;

Debug::enable();

The enable() method registers an error handler, an exception handler and a special class loader.

Read the following sections for more information about the different available tools.

Caution

You should never enable the debug tools in a production environment as they might disclose sensitive information to the user.

Enabling the Error Handler

The ErrorHandler class catches PHP errors and converts them to exceptions (of class ErrorException or FatalErrorException for PHP fatal errors):

use Symfony\Component\Debug\ErrorHandler;

ErrorHandler::register();

Enabling the Exception Handler

The ExceptionHandler class catches uncaught PHP exceptions and converts them to a nice PHP response. It is useful in debug mode to replace the default PHP/XDebug output with something prettier and more useful:

use Symfony\Component\Debug\ExceptionHandler;

ExceptionHandler::register();

Note

If the HttpFoundation component is available, the handler uses a Symfony Response object; if not, it falls back to a regular PHP response.

Debugging a Class Loader

The DebugClassLoader attempts to throw more helpful exceptions when a class isn't found by the registered autoloaders. All autoloaders that implement a findFile() method are replaced with a DebugClassLoader wrapper.

Using the DebugClassLoader is as easy as calling its static enable() method:

use Symfony\Component\Debug\DebugClassLoader;

DebugClassLoader::enable();

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.