Skip to content
Justin Hileman edited this page Jun 3, 2017 · 12 revisions

πŸ–₯ Usage

PsySH as a REPL

PsySH functions as a REPL for PHP right out of the box! Once you've installed PsySH, running it directly (psysh or ./psysh) will drop you into an interactive prompt, like so:

~ $ ./psysh
Psy Shell v0.1.0-dev (PHP 5.4.9-4ubuntu2.2 β€” cli) by Justin Hileman
>>>

From here, you can type PHP code and see the result interactively:

>>> function timesFive($x) {
...     $result = $x * 5;
...     return $result;
... }
=> null
>>> timesFive(10);
=> 50
>>>

PsySH as a Debugger

To use PsySH as a debugger, install it as a Composer dependency or include the Phar directly in your project:

require('/path/to/psysh');

Then, drop this line into your script where you'd like to have a breakpoint:

eval(\Psy\sh());

… which is just a shorter way of saying:

extract(\Psy\Shell::debug(get_defined_vars()));

When your script reaches this point, execution will be suspended and you'll be dropped into a PsySH shell. Your program state is loaded and available for you to inspect and experiment with.

Pro Tipβ„’: You don't have to use get_defined_vars… You can pass anything you want in as your debugging context:

$result = \Psy\Shell::debug(['app' => $myApp]);

If you're starting the debug shell from inside a class context, you can pass an optional second argument to add a bound object to the shell. This is super useful because you'll be able to call things on $this inside your debug shell, and you will have full access to private and protected members of your current context:

\Psy\Shell::debug(get_defined_vars(), $this);

If you call the shortcut eval(\Psy\sh()) from inside a class context, you'll get $this bound for free.

More usage info

Clone this wiki locally