A Few Words About Debugging – Part II
Last time, in the article “A few words about debugging - Part I”, we talked about the best PHP configuration for Magento debugging. Today we'll discuss the extensions that will help you when the code goes astray.
There are many tools which are helpful in debugging and profiling the PHP code:
A must-have for me. Usually Xdebug doesn't need any additional configuration. It enables stack-trace display for every error, protects against the unwanted recursion and improves the “var_dump” function out of the box. What's more, Xdebug is highly configurable, has numerous useful features, a profiler, and the option to integrate a debugger.
What's really important is that profiling with Xdebug is a breeze. Right after the launch it creates a log of every server query, which can be later reviewed and analyzed with KcacheGrind/QCacheGrind. This can be used to check which elements/functions use the most of the server resources.
Kint is a PHP library which simplifies PHP debugging. It shares some additional functions and methods for presenting the variables, objects and traces in a more accessible way. The installation process is easy and no additional server permissions are required: all you need to do is to add an extra file to the script.
Debugging the global variables with Kint:
Stack-trace from our previous example:
whoops is yet another library which simply works out of the box. Just add it to a project for this neat error and exception handling:
Even though whoops doesn't offer any fancy features, it lets you take a look at the code from each stack-trace element. Often it's not even necessary to open the editor to see what causes an error.
XHProf, a PHP extension created by Facebook, can be used to profile the code. The results are visible in the browser. XHProf is a good alternative to Xdebug and KcacheGrind combination.
In Part III we'll share with you a handful of best coding practices. Stay tuned!