The Debug Component
The Debug component provides tools to ease debugging PHP code.
Installation
- $ composer require symfony/debug
Note
If you install this component outside of a Symfony application, you mustrequire the vendor/autoload.php
file in your code to enable the classautoloading mechanism provided by Composer. Readthis article for more details.
Usage
The Debug component provides several tools to help you debug PHP code.Enable all of them by calling this method:
- use Symfony\Component\Debug\Debug;
- Debug::enable();
The enable()
method registers anerror handler, an exception handler anda special class loader.
Read the following sections for more information about the different availabletools.
Caution
You should never enable the debug tools, except for the error handler, in aproduction environment as they might disclose sensitive information to the user.
Enabling the Error Handler
The ErrorHandler
class catches PHP errorsand converts them to exceptions (of class ErrorException
orFatalErrorException
for PHPfatal errors):
- use Symfony\Component\Debug\ErrorHandler;
- ErrorHandler::register();
This error handler is enabled by default in the production environment when theapplication uses the FrameworkBundle because it generates better error logs.
Enabling the Exception Handler
The ExceptionHandler
class catchesuncaught PHP exceptions and converts them to a nice PHP response. It is usefulin debug mode to replace the default PHP/XDebug output with something prettierand more useful:
- use Symfony\Component\Debug\ExceptionHandler;
- ExceptionHandler::register();
Note
If the HttpFoundation component isavailable, the handler uses a Symfony Response object; if not, it fallsback to a regular PHP response.
Debugging a Class Loader
The DebugClassLoader
attempts tothrow more helpful exceptions when a class isn't found by the registeredautoloaders. All autoloaders that implement a findFile()
method are replacedwith a DebugClassLoader
wrapper.
Using the DebugClassLoader
is done by calling its staticenable()
method:
- use Symfony\Component\Debug\DebugClassLoader;
- DebugClassLoader::enable();