PHP

Debugging your PHP Applications


Currently, there are several PHP developers that try to debug using var_dump. Common we are in 2015, the year of THE SLAYER and DRAGON BLADE. It’s time to wield that sword in different directions to see what’s possible in PHP.

One way of debugging your PHP applications is using XDebug.

XDebug is fantastic, it can be enabled on the machine hosting your PHP engine and also integrated with Sublime Text. I’ll elaborate on that in a later post.

Another way of debugging your PHP applications is via your Chrome Browser.

I know you might be thinking. How did Heck is this possible? For many of you that do Javascript regularly, 98% of your debugging is from the console.

Console.log practically saves the life of every Javascript Developer and then you can inspect every element, constructor, objects and all of that from your Console in the Browser.

The same technique of debugging is also very possible in PHP with the Clockwork Chrome Extension.

Clockwork provides  all kinds of information useful for debugging and profiling your PHP scripts, including information on request, headers, GET and POST data, cookies, session data, database queries, routes, visualisation of application runtime and more.

Installation

For a normal PHP app, head over to the Chrome web store and download it, then install.

Note: You also need to install a server-side component for it to work.

So go ahead and require it in your PHP project like so:

..and then make sure it is loaded at runtime by referencing the autoload file in your PHP scripts

Clockwork provides out of the box support for Laravel.

1. Require the clockwork package like we did above.

2. Register the Service Provider in your config/app.php like so:

When using Laravel 5, you need to add Clockwork middleware, in your app/Http/Kernel.php like so:

By default, Clockwork will only be available in debug mode, you can change this and other settings in the configuration file. Use the following Artisan command to publish the configuration file into your config directory:

For Laravel 4 you can do the same with this command:

Clockwork also comes with a facade, which provides an easy way to add records to the Clockwork log and events to the timeline. You can register the facade in your config/app.php like so:

Now you can use the following commands:


Clockwork provides out of the box support for Lumen.

Once Clockwork is installed, you need to register the Clockwork service provider, in your bootstrap/app.php like so:

You also need to add the Clockwork middleware, in the same file:


By default, Clockwork will only be available in debug mode (APP_DEBUG set to true), you can change this and other settings via environment variables. Simply specify the setting as environment variable prefixed with CLOCKWORK_, eg. CLOCKWORK_ENABLE, full list of available settings.

Clockwork also comes with a facade, which provides an easy way to add records to the Clockwork log and events to the timeline. The facade will be automatically registered when you enable facades for your Lumen app, in bootstrap/app.php like so:


Now you can use the following commands:


Clockwork provides out of the box support for Slim 2.

Once Clockwork is installed, you need to add Slim middleware to your app:


Clockwork is now available in Slim’s DI container and can be used like this:


Clockwork provides out of the box support for CodeIgniter 2.1

Once Clockwork is installed, you need to copy the Clockwork controller from vendor/itsgoingd/clockwork/Clockwork/Support/CodeIgniter/Clockwork.php to your controllers directory and set up the following route:


Finally, you need to set up the Clockwork hooks by adding following to your application/config/hooks.php file:


To use Clockwork within your controllers/models/etc. you will need to extend your CI_Controllerclass. (If you haven’t done so already) Create a new file at application/core/MY_Controller.php.


Now you can use the following commands in your CodeIgniter app:


Please, if you have any questions or better ways of debugging, do not hesitate to drop it in the comments section below.

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.