PHP for Pirates — Interactive Debugging in PHP

@bobthecow

Presentate

AARRRRGH

Interactive debugging in PHP

Stealing the good bits from Ruby and Javascript

My name is Justin

@bobthecow

The bad old days

var_dump($data);
die();

Effective

Debugging

• Find broken code.
• Figure out why.
• Fix it.
• Repeat.

The state of the art

var_dump($data);
die();

It’s not just PHP

Perhaps there are
no alternatives?

Step debuggers

It gets better.

It gets even better.

Perhaps it’s a
limitation in PHP?

Runtime interaction

debugger;

Reflection

{}.methods

=> [:rehash, :to_hash, :to_a, :inspect, :to_s, :==, :[], :hash, :eql?, :fetch, :[]=, :store, :default, :default=, :default_proc, :default_proc=, :key, :index, :size, :length, :empty?, :each_value, :each_key, :each_pair, :each, :keys, :values, :values_at, :shift, :delete, :delete_if, :keep_if, :select, :select!, :reject, :reject!, :clear, …
{}.methods.select { |m| m =~ /sort/ }

=> [:sort, :sort_by]
$r = new ReflectionObject(new ArrayObject);

$methods = array();
foreach ($r->getMethods() as $m) {
    if (strpos($m->name, 'sort') !== false) {
        $methods[] = $method->name;
    }
}

var_dump($methods);
array(6) {
  [0]=>
  string(5) "asort"
  [1]=>
  string(5) "ksort"
  [2]=>
  string(6) "uasort"
  [3]=>
  string(6) "uksort"
  [4]=>
  string(7) "natsort"
  [5]=>
  string(11) "natcasesort"
}

Self-documentation

{}.keys.__doc__

=> "D.keys() -> list of D's keys"



{}.pop.__doc__

=> "D.pop(k[,d]) -> v, remove specified key and return the corresponding value. If key is not found, d is returned if given, otherwise KeyError is raised"
$r = new ReflectionObject(new ArrayObject);
$m = $r->getMethod('append');

$m->getDocComment();

=> false

Perhaps it’s just
too much friction?

• Isolate broken code.
• Gather information fast.
• Limit context switching.

die('here');

Let’s steal something better

Read.
Eval.
Print.
Loop.

$ php -a

$ php -a

AARRRRGH

Put the
AARRRRGH
in REPL

Firebug

+ WebKit Inspector
+ Opera Dragonfly
+ Firefox DevTools
+ Internet Explorer Developer Tools

$ python

$ irb

$ pry

So where’s PHP’s real REPL?

$ phpsh

Introducing PsySH

$ wget psysh.org/psysh
$ chmod +x psysh
$ ./psysh

So let’s see this bad boy.

But why is this cool?

Try it instead of Googling it

Debug interactively

Read docs in context

Get feedback faster

• Find broken code.
• Figure out why.
• Fix it.
• Repeat.

psysh.org

Liked this talk? Share it!

  •   / 63

Published on , Last updated:

Share this presentation