PsySH -- PHP interactive console PsySH
PsySH is a runtime developer console, interactive debugger and REPL for PHP.
PsySH is a PHP runtime development platform, interactive debugger and Read-Eval-Print Loop (REPL ).
Just like debugging your JavaScript code with the firebug console.
- PsySH official website
- GitHub
- Packagist
Install
The official website introduces three installation methods:
- Direct download
- Install Composer
- Code for direct cloneGitHub repository
I recommend that you install Composer, because this project has other dependent projects, which can be well solved with Composer.
The following tutorial uses OS X and Windows 10 as an example. assume that php and Composer have been installed and they have been set as system environment variables:
OS X
Download PsySH with Composer. here we use global installation:
$ composer global require psy/psysh
After installation, PsySH has been installed in the/Users/{User name}/. composer/vendor/psy/psysh directory.
In this case, you can directly run:
$/Users/{User name}/. composer/vendor/psy/psysh/bin/psysh
For ease of use, it is recommended to add it to the environment variable:
$ Echo 'export PATH = "/Users/{User name}/. composer/vendor/psy/psysh/bin: $ PATH" '>> ~ /. Bashrc $ source ~ /. Bashrc
Windows
Similarly, to use Composer for installation, press win + R, enter cmd, open windows console, and then:
composer global require psy/psysh
After the installation is complete, PsySH is installed to C: Users {User name} AppDataRoamingComposervendorpsypsysh
Because the bin/psysh file is not an executable file for windows, run the following command to run PsySH:
Php C: Users {User name} AppDataRoamingComposervendorpsypsyshbinpsysh
For ease of use, create a file named psysh. bat in the C: Users {User name} AppDataRoamingComposervendorpsypsyshbin Directory. the content of the file is as follows:
@ECHO OFFphp "%~dp0psysh" %*
In this case, add the C: Users {User name} AppDataRoamingComposervendorpsypsyshbin to the system's environment variable PATH, and then run psysh directly in cmd:
C:UsersVergil>psyshPsy Shell v0.6.1 (PHP 5.6.8 — cli) by Justin Hileman>>>
Features
As mentioned above, PsySH is an interactive PHP running console. here, you can write php code to run and clearly see the returned values each time:
In addition, it intelligently knows whether your code has ended.
Automatic Completion
PsySH can be used to automatically complete variable names, functions, classes, methods, attributes, and even files by pressing the two keys as in the console:
Document
What should I do if I forget the parameter during runtime? PsySH documentation allows you to view documents in real time.
PsySH files are stored in ~ /. Local/share/psysh /. (Stored in C: Users {User name} AppDataRoamingPsySH in windows)
Documentation:
- English
- Brazilian Portuguese
- Chinese (Simplified)
- French
- German
- Italian
- Japan
- Polish
- Romanian
- Russian
- Persian
- Spanish
- Turkish
Download Chinese documents:
$ cd ~/.local/share $ mkdir psysh$ cd psydh$ wget http://psysh.org/manual/zh/php_manual.sqlite
OK. Then, open PsySH again.
It also comes with the paging function (the same as the more command)
Of course, custom objects can also have the following functions:
For example, the file test. php contains the following content:
View source code
It is easy to show the source code of any user-level object, class, interface, feature, constant, method or attribute:
Next, we will use the following example:
View the show help and run the help show command:
Reflection list
The list command knows all your code-and others. You can easily list and search for all variables, constants, classes, interfaces, features, functions, methods, and attributes.
For more information, enter the help list command.
Obtain the final exception information
If you forget the catch exception, you can use the wtf command (what does the fuck mean by wtf ?) View exception information:
History
You can view your PHP code or command in PsySH, just like the history Command in a Unix-like system. Run the help history Command to view details.
Exit
Use the exit command to exit your PsySH
Help
The best command, help or ?, It helps you understand all the commands
Debug in built-in web server
I originally wanted to use Laravel as an example, but the content of debugging often needs to be viewed on multiple pages. Well, ThinkPHP is used as an example:
First, install ThinkPHP
Then, use Composer to locally install PsySH:
Because the dump () function of ThinkPHP conflicts with the dump () function of symfony/var-dumper, you need to rename one of them. I suggest you change the TP. Why? Because symfony is much easier to use...
Add the following three codes to the index. php file:
In the project root directory, use the PHP built-in web server to start:
php -S localhost:8080
Then you can access your application in the browser: http: // localhost: 8080
You will see the following picture, PsySH started
Other operations are similar to CLI operations. For example:
ls -al ThinkModel
show \Think\Model::find
doc \Think\Model::save
Of course, the PsyShell: debug () does not have to be placed in the entry file. it can be placed in all the places you want to debug. for example, I put it in the default homepage controller:
Run the php built-in server again and access the page. Then, enter the following command:
ls -al
After PsySH is enabled, enter get_defined_vars () to try?