psy/psysh
PsySH is an interactive PHP REPL and runtime developer console for debugging and exploring code. Inspect variables, run snippets, and get contextual help in a powerful shell, with configuration, themes, and integrations available.
Installation:
composer require psy/psysh
For Laravel projects, use laravel/tinker (built on PsySH) or install PsySH directly.
First Use:
vendor/bin/psysh
Or in Laravel:
php artisan tinker
First Command:
$user = App\Models\User::first();
$user->name; // Inspect property
$_, $_E, $_S), history, and shell integration.$_ for last result, $_E for last exception).ls, doc, edit, and copy.// Load a model
$user = App\Models\User::find(1);
// Inspect properties
$user->name;
// Call a method interactively
$user->posts()->count();
// Use magic variables
$_ // Last result (count)
$_E // Last exception (if any)
Interactive Debugging:
ls to list methods/properties of an object:
$user = App\Models\User::first();
ls($user); // Lists all methods/properties
show:
show($user->posts); // Detailed output
Code Exploration:
doc to view PHPDoc for a class/method:
doc(App\Models\User); // Shows class docblock
$user-> → press Tab to list methods).Quick Tests:
$valid = Validator::make(['email' => 'test@example.com'], ['email' => 'required|email']);
$valid->fails(); // Check validation
$_ // Last result (bool)
Shell Integration:
!! to execute shell commands:
!!ls -la // Lists files in the current directory
echo "<?php return 1 + 1;" | vendor/bin/psysh
Editing Code:
edit to open a file in $EDITOR:
edit(app_path('Http/Controllers/UserController.php'));
uopz extension):
Modify a file and switch back to PsySH—changes apply instantly.Configuration:
config('useSyntaxHighlighting', false);
.psysh.php (project root):
return [
'useExperimentalReadline' => true,
'theme' => 'monokai',
];
php artisan tinker (built on PsySH) for seamless integration with Laravel’s service container.Psy\Command\Command.composer.json autoloads are visible in PsySH (check autoload_psr-4 in .psysh.php if needed).PSYSH_CONFIG env var to point to a custom config file:
PSYSH_CONFIG=config/psysh-local.php vendor/bin/psysh
Trust Project Warnings:
.psysh.php) or Composer autoloads in untrusted directories.vendor/bin/psysh --trust-project or set 'trustProject' => 'always' in config.--no-trust-project to skip checks.Hot Reloading Limitations:
yolo to bypass safety checks).uopz extension (pecl install uopz).Experimental Readline:
--experimental-readline or 'useExperimentalReadline' => true.config('useExperimentalReadline', false).Magic Variables Overwritten:
$_, $_E, $_S are overwritten on each command. Use show($_) to inspect the last result persistently.Pager Issues:
less) is closed early. Use --no-pager to disable:
vendor/bin/psysh --no-pager
Windows Line Endings:
dos2unix or configure PsySH to handle CRLF line endings in .psysh.php:
'lineEnding' => "\n", // Force LF
Inspect PsySH State:
psy\info() to check config, readline state, and environment:
psy\info();
--info flag for CLI output:
vendor/bin/psysh --info
Enable Verbose Mode:
vendor/bin/psysh -vvv
config('verbosity', 3);
Clipboard Issues:
copy command fails, configure clipboardCommand in .psysh.php:
'clipboardCommand' => 'pbcopy', // macOS
// 'clipboardCommand' => 'xclip -selection clipboard', // Linux
// 'clipboardCommand' => 'clip', // Windows
'useOsc52Clipboard' => true,
Command Ambiguity:
; to force PHP execution (e.g., ;config('key', 'value') instead of config('key', 'value')).Performance:
config('useSyntaxHighlighting', false);
compact output for deep objects:
config('compactOutput', true);
Custom Commands:
Psy\Command\Command:
namespace App\PsySH;
use Psy\Command\Command;
class MyCommand extends Command {
public function __invoke() {
$this->output->writeln('Hello from custom command!');
}
}
.psysh.php:
return [
'commands' => [
'mycmd' => App\PsySH\MyCommand::class,
],
];
Themes:
Psy\Theme\Theme and add to .psysh.php:
'theme' => App\PsySH\CustomTheme::class,
Exception Details:
exceptionDetails callback:
'exceptionDetails' => function (\Throwable $e) {
return "Validation errors: " . json_encode($e->validator->errors());
},
Pre-Initialization:
PSYSH_PRE_INIT in .psysh.php:
'preInit' => [
'App\PsySH\Bootstrap::init',
],
.psysh.php:
'aliases' => [
'u' => 'App\Models\User',
'p' => 'App\Models\Post',
];
u::first() works instead of `App\ModelsHow can I help you explore Laravel packages today?