I see a lot of people using things like on the command line:

drush ev 'var_dump(\Drupal\user\Entity\User::load(1));'

Aside from looking like crap, you need to type drush ev ... each time, remember to dump or print the output, escape quotes, and so on.

Or using The devel module’s /devel/php form. Which works OK, you could use dpm() for some nicer output. This approach also has downsides. A big one being that you have to have devel enabled, and an execute PHP permission if you’re not user 1. This would not be the best idea on a production system, for example.

There is a better way…

Since Drush 7.0.0 we now have a drush php command at our disposal. Let’s use it! You can then use an interactive PHP REPL with your bootstrapped site. So you could run the same as above, but you don’t need to print the output, and it’s evaluated for you. It’s a Drupal code playground. You can do quick code experimentation, or just grab some data. This can also help with debugging certain issues.

We recently merged a pull request to use PsySH as our underlying REPL, instead of Boris. This post will cover usage with PsySH. Boris does not have the rich feature set that PsySh does, but a lot of this still applies if you are using Drush 7. Otherwise, get the latest Drush master and try out these great new features. If you are on OSX you can easily do this with homebrew: brew install drush --HEAD.

Here is a quick sample:

Drush PHP intro

You can create your own local variables to (re)use inside the REPL. Everything is evaluated and printed for you. Also, notice how using semi-colons is optional.

Drush PHP comment load

In Drupal 8 (as the example above is), you get the service container added into the variable scope for you, so you don’t need to do \Drupal::getContainer() or \Drupal::service('views.view_data'). You can just do $container->get('views.views_data').

However, you’ll notice that you can’t really see much information about the object right now, as all the properties are protected. This is no problem for PsySH - you can dump properties of the object easily, with the dump command:

dump -a $variable

Drush PHP comment dump

As you can see, you also get some easy on the eye colours, property visibility, and formatting. PsySH uses elements of the Symfony VarDumper component. Much better than the standard PHP output. We get the best of both worlds, we can see what object we are dealing with without always dumping everything to the output.

You can also see further information about the class using the ls command. For example,

ls -la \Drupal\user\Entity\User

will show you all User entity constants, properties, and methods with their signature.

To save your fingers even more, PsySH also supports autocompletion. You get autocompletion on functions, namespaces, object methods, and any local variables you have set up in your session. Not too shabby!

Another really useful feature is being able to work in namespaces. Set the namespace and work in that context:

Drush PHP namespace

You can also show the code for a method using the show command, or the docblock using the doc command:

Drush PHP code and docs

Hopefully that gives you an idea of all the new power you have at your fingertips using PsySH and drush php.

Give it a try!