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:
1 |
composer require itsgoingd/clockwork |
..and then make sure it is loaded at runtime by referencing the autoload file in your PHP scripts
1 |
require 'vendor/autoload.php' |
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:
1 2 3 4 |
'providers' => array( ... 'ClockworkSupportLaravelClockworkServiceProvider::class' ) |
When using Laravel 5, you need to add Clockwork middleware, in your app/Http/Kernel.php like so:
1 2 3 4 |
protected $middleware = [ 'ClockworkSupportLaravelClockworkMiddleware', ... ] |
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:
1 2 |
$ php artisan vendor:publish --provider='ClockworkSupportLaravelClockworkServiceProvider' |
For Laravel 4 you can do the same with this command:
1 2 |
$ php artisan config:publish itsgoingd/clockwork --path vendor/itsgoingd/clockwork/Clockwork/Support/Laravel/config/ |
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:
1 2 3 4 5 6 7 8 9 |
Clockwork::startEvent('event_name', 'Event description.'); // event called 'Event description.' appears in Clockwork timeline tab Clockwork::info('Message text.'); // 'Message text.' appears in Clockwork log tab Log::info('Message text.'); // 'Message text.' appears in Clockwork log tab as well as application log file Clockwork::info(array('hello' => 'world')); // logs json representation of the array Clockwork::info(new Object()); // logs string representation of the objects if the object implements __toString magic method, logs json representation of output of toArray method if the object implements it, if neither is the case, logs json representation of the object cast to array Clockwork::endEvent('event_name'); |
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
:
1 |
$app->register(ClockworkSupportLumenClockworkServiceProvider::class); |
You also need to add the Clockwork middleware, in the same file:
1 2 3 4 |
$app->middleware([ ... ClockworkSupportLumenClockworkMiddleware::class ]); |
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
:
1 |
$app->withFacades(); |
Now you can use the following commands:
1 2 3 4 5 6 7 8 9 |
Clockwork::startEvent('event_name', 'Event description.'); // event called 'Event description.' appears in Clockwork timeline tab Clockwork::info('Message text.'); // 'Message text.' appears in Clockwork log tab Log::info('Message text.'); // 'Message text.' appears in Clockwork log tab as well as application log file Clockwork::info(array('hello' => 'world')); // logs json representation of the array Clockwork::info(new Object()); // logs string representation of the objects if the object implements __toString magic method, logs json representation of output of toArray method if the object implements it, if neither is the case, logs json representation of the object cast to array Clockwork::endEvent('event_name'); |
Clockwork provides out of the box support for Slim 2.
Once Clockwork is installed, you need to add Slim middleware to your app:
1 2 |
$app = new Slim(...); $app->add(new ClockworkSupportSlimClockworkMiddleware('/requests/storage/path')); |
Clockwork is now available in Slim’s DI container and can be used like this:
1 2 3 4 5 6 7 8 |
$app = Slim::getInstance(); $app->clockwork->startEvent('event_name', 'Event description.'); // event called 'Event description.' appears in Clockwork timeline tab $app->clockwork->info('Message text.'); // 'Message text.' appears in Clockwork log tab $app->log->info('Message text.'); // 'Message text.' appears in Clockwork log tab as well as application log file $app->clockwork->endEvent('event_name'); |
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:
1 |
$route['__clockwork/(.*)'] = 'clockwork/$1'; |
Finally, you need to set up the Clockwork hooks by adding following to yourÂ
application/config/hooks.php
file:
1 |
ClockworkSupportCodeIgniterRegister::registerHooks($hook); |
To use Clockwork within your controllers/models/etc. you will need to extend your
CI_Controller
class. (If you haven’t done so already) Create a new file at application/core/MY_Controller.php
.
1 2 3 4 5 6 7 8 |
class MY_Controller extends CI_Controller { public function __construct() { parent::__construct(); $GLOBALS['EXT']->_call_hook('pre_controller_constructor'); } } |
Now you can use the following commands in your CodeIgniter app:
1 2 3 4 5 |
$this->clockwork->startEvent('event_name', 'Event description.'); // event called 'Event description.' appears in Clockwork timeline tab $this->clockwork->info('Message text.'); // 'Message text.' appears in Clockwork log tab $this->clockwork->endEvent('event_name'); |
Please, if you have any questions or better ways of debugging, do not hesitate to drop it in the comments section below.

- How to build your own Youtube – Part 10 - August 1, 2016
- How to build your own Youtube – Part 9 - July 25, 2016
- How to build your own Youtube – Part 8 - July 23, 2016
- How to build your own Youtube – Part 6 - July 6, 2016
- Introducing Laravel Password v1.0 - July 3, 2016
- How to build your own Youtube – Part 5 - June 28, 2016
- How to build your own Youtube – Part 4 - June 23, 2016
- How to build your own Youtube – Part 3 - June 15, 2016
- How to build your own Youtube – Part 2 - June 8, 2016
- How to build your own Youtube – Part 1 - June 1, 2016