PSY Shell: The PHP REPL I’ve been looking for

I’m a huge fan of REPLs for testing and playing with little bits of code. Mostly for playing with weird functions I’ve found on Twitter or in the standard libraries of some of the world’s most popular languages.

If you’re not familiar with REPLs, they’re basically environments that will Read and Evaluate whatever code you throw into them, and then Print the return value of your statement and Loop back to await more code. Chances are that you’ve used the JavaScript REPL built into your browser devtools. It looks like this:

If you’ve got Node installed on your computer, it actually comes with a REPL built in. You can access it by typing node into the command line:

PHP will also install something like a REPL when it’s installed on your computer. You can access it by running php -a in the console. I say something like because PHP doesn’t actually print the return value of whatever statement you’ve passed in. Instead, it evaluates the PHP code literally.

This has long been a bit of an issue for me, since if I just wanted to try out some code, or see how a function works, I’d always wind up wrapping everything in a var_dump; but then the output wouldn’t be nicely formatted and I’d have to scroll back through some ugly array to find what I was looking for.

But no more

I’ve not made a secret around the office of my new appreciation for the Laravel Framework (though if I’m honest I’m a little leery of all this ‘web artisan’ talk). Laravel includes in its standard install a small package called tinker, which is essentially a REPL hooked up to your Laravel application’s bootstrap logic. This means you can access your Eloquent models, events, send mail, or run tasks through the command line. And it’s a bona fide REPL, as well: it prints return values, pretty-prints arrays and objects, and even gives you some simple syntax highlighting. Thanks, tinker!

The downside here is that tinker is only available in Laravel applications. You can’t run tinker globally. Or can you?

A quick glance through the tinker documentation reveals (in the very first sentence no less) that tinker is actually just a wrapper around Psy Shell, a full-featured REPL for PHP. It’s installable as a composer global package and it works without trouble right out of the box. It has support for autocomplete, and documents your code as you code, so you don’t have to keep tabbing back over to VS Code when you forget your function’s argument order for the umpteenth time.

There’s a ton more that Psy Shell can do, so I recommend you head over to The Official Psy Shell Website and give their docs a quick read.

I breathe a sigh of relief: I expect that I am never going to type php -a again.

About The Author

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This post doesn't have any comment. Be the first one!

hide comments

Registered address: CRTR LTD, 30 Yoden Way, Peterlee, County Durham, SR8 1AL Incorporated in England and Wales as a private limited company Registered number 10035216

...

This is a unique website which will require a more modern browser to work!

Please upgrade today!