Justin Hileman edited this page Nov 25, 2017 · 7 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

PsySH can be used to set a breakpoint within a script at which point it will pause and provide access to the shell to inspect variables and run commands in the context of where the breakpoint was located. At the current time it does not support step-through debugging (like xdebug) but is also less complex with less setup required.

Unfortunately, debugging through a web server like Apache or nginx or PHP-FPM is also not supported. Debugging is only supported when running PHP scripts from the command line or through PHP's built-in web server. If you're interested in helping add this functionality, see Github issue 121.

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\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\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\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

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.