This article is about how to survive to PHP coding after having switched to Python for a long time. Interactive console and full featured step debugger will save you from unrecoverable sadness.
I’ve spent more than seven years programming PHP, when finally switched to Python my productivity increased at least by a factor of 5 and of of the the main reasons was the much more powerful debuggers and shells (IPython to say one). From time to time I still have to do some PHP coding and I still find myself trying to improve my PHP debugging experience.To die() or not to die()
I’ve been a kind ofvar_dump(); die();
programmer since I discovered x-debug
and it’s advanced var_dump()
and automatic stack trace. With those tools I’ve been able to debug medium complexity web apps, the kind that was widespread some ten years ago.
With the increased complexity of modern web applications, the echo and die approach might not be enough.
Eclipse to the rescue
Recently I’ve found two usable tools, not yet close to what we’ve got with Python but still worth to give a try. First came Eclipse PHP Development Tools, this is an eclipse plugin I’ve been trying several times during the years without being completely satisfied. Now I believe that it has reached a good level of maturity and stability, if you need a full featured step debugger, it’s probably the right choice for you. To set it up, just google for one of the available guides.REPL for a real console
What I still missed was an interactive console that I could fire when needed in the middle of the code, just like I normally do withfrom IPython import embed; embed()
.
Today I found a nice tool that allows you to have something similar in PHP, of course I had to switch from apache to the PHP webserver and forget apache and the production environment clone I’m normally developing on.
You can launch the PHP development server with:
php -S localhost:8000The tool I’m talking about is PsySH: A runtime developer console, interactive debugger and REPL for PHP. Installation wasn’t that hard after installing another new (at least for me, coming from the PEAR age) tool: composer
curl -sS https://getcomposer.org/installer | php -- --install-dir=/home/xxx/bin composer g require psy/psysh:@stableYou can then place a breakpoint with these lines:
require '/home/xxx/.composer/vendor/autoload.php'; \Psy\Shell::debug(get_defined_vars());This is what you get in the console after interrupting the program:
Psy Shell v0.2.1 (PHP 5.5.9-1ubuntu4.4 — cli-server) by Justin Hileman >>> >>> >>> $db = JFactory::getDbo(); => { name: "mysqli" } >>> $db->quoteName("#__extensions"); => "`#__extensions`" >>> ^D Exit: Ctrl+D