
Avendo lavorato per 8 anni quasi solo con PHP, quando sono finalmente passato a Python la mia produttività è andata alle stelle. Ma ogni tanto PHP ritorna sotto i miei polpastrelli e quindi ritornano i tentativi di migliorare il debugging. In questo articolo descrivo alcune tecniche che mi hanno permesso di non perdere la calma…
Questo articolo è disponibile solo in inglese, puoi cambiare la lingua utilizzando le bandierine in alto
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.
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