Table of Contents
PsySH
安装
OS X
Windows
特性
自动完成
文档
查看源代码
反射列表
获取最后的异常信息
历史记录
退出
help
在内置web server中调试
Home Backend Development PHP Tutorial PsySH--PHP交互式控制台

PsySH--PHP交互式控制台

Jun 20, 2016 pm 12:33 PM

PsySH

PsySH is a runtime developer console, interactive debugger and REPL for PHP.

PsySH是一个PHP的运行时开发平台,交互式调试器和Read-Eval-Print Loop (REPL)。

说的简单点,就像你用firebug的console调试你的JavaScript代码一样。

  • PsySH官网
  • GitHub
  • Packagist

安装

官网介绍了3种安装方式:

  • 直接下载
  • Composer安装
  • 直接cloneGitHub仓库的代码

我比较建议选择Composer安装,因为这个项目还有其他的依赖项目,用Composer很好解决这个问题。

以下教程以OS X和Windows 10为例,假定已经安装php和Composer并已经将它们设置为系统环境变量:

OS X

先用Composer下载PsySH,这里用的是全局安装:

$ composer global require psy/psysh
Copy after login

安装完毕后,PsySH已经安装到/Users/{用户名}/.composer/vendor/psy/psysh目录下

此时,你可以直接运行:

$ /Users/{用户名}/.composer/vendor/psy/psysh/bin/psysh
Copy after login

为了使用方便,建议将它加入到环境变量:

$ echo 'export PATH="/Users/{用户名}/.composer/vendor/psy/psysh/bin:$PATH"' >> ~/.bashrc$ source ~/.bashrc
Copy after login

Windows

同样地,使用Composer安装,按win键+R键,输入cmd,打开windows控制台,然后:

composer global require psy/psysh
Copy after login

安装完成后,PsySH被安装到C:Users{用户名}AppDataRoamingComposervendorpsypsysh

因为bin/psysh文件并不是windows的可执行文件,所以需要使用以下命令运行PsySH

php C:Users{用户名}AppDataRoamingComposervendorpsypsyshbinpsysh
Copy after login

为了使用方便,在C:Users{用户名}AppDataRoamingComposervendorpsypsyshbin目录下新建一个名为psysh.bat的文件,其内容如下:

<a href="http://www.jobbole.com/members/echo">@ECHO</a> OFFphp "%~dp0psysh" %*
Copy after login

此时,把C:Users{用户名}AppDataRoamingComposervendorpsypsyshbin加入到系统的环境变量PATH,以后可以直接在cmd下运行psysh了:

C:UsersVergil>psyshPsy Shell v0.6.1 (PHP 5.6.8 — cli) by Justin Hileman>>>
Copy after login

特性

上面说过,PsySH是一个交互式的PHP运行控制台,在这里,你可以写php代码运行,并且可以清楚看到每次的返回值:

并且,它很智能地知道你的代码是否已经结束

自动完成

PsySH可以像控制台那样,按下两次键自动补全,帮你自动完成变量名,函数,类,方法,属性,甚至是文件:

文档

在运行时忘记参数怎么办?PsySH的文档功能可以让你即时查看文档。

PsySH的文档存放在~/.local/share/psysh/。(windows系统存放在C:Users{用户名}AppDataRoamingPsySH)

文档下载地址:

  • English
  • Brazilian Portuguese
  • Chinese (Simplified)
  • French
  • German
  • Italian
  • Japanese
  • Polish
  • Romanian
  • Russian
  • Persian
  • Spanish
  • Turkish

下载中文文档:

$ cd ~/.local/share $ mkdir psysh$ cd psydh$ wget http://psysh.org/manual/zh/php_manual.sqlite
Copy after login

OK,完成后重新打开PsySH

而且还自带分页功能(和more命令一样)

当然,自定义的对象也可以有相关功能:

比如,test.php这个文件有如下内容:

查看源代码

轻松展现任何用户级的对象,类,接口,特质,常数,方法或属性的源代码:

接着上一个例子:

查看show的帮助,使用help show命令:

反射列表

list命令知道所有关于你的代码 – 和其他人的。轻松地列出并搜索所有的变量,常量,类,接口,特点,功能,方法和属性。

更多用法,可以输入help list命令查看。

获取最后的异常信息

如果忘记catch异常,可以使用wtf命令(wtf是what the fuck的意思么?)查看异常的信息:

历史记录

可以像类Unix系统的history命令一样,在PsySH可以查看你运行过的PHP代码或命令。详情运行help history命令查看。

退出

使用exit命令退出你的PsySH

help

最好用的命令,help或者?,它能帮助你了解一切的命令

在内置web server中调试

本来想用Laravel做示例,但debugging的内容经常过多要分页查看。而且说好的中文文章,好吧,那就用ThinkPHP为例:

首先,安装一下ThinkPHP

然后,用Composer局部安装一下PsySH:

因为ThinkPHP的dump()函数和symfony/var-dumper的dump()函数有重名冲突,所以需要把其中一个重命名。个人建议把TP的改掉。为啥?因为symfony的好用多了。。。

在入口文件index.php添加如下3句代码:

在项目根目录下,用PHP的内置web服务器启动:

php -S localhost:8080
Copy after login

然后即可在浏览器访问你的应用: http://localhost:8080

你将会看到以下画面,PsySH启动了

其他操作,和CLI操作差不多。例如:

ls -al ThinkModel
Copy after login

show \Think\Model::find
Copy after login

doc \Think\Model::save
Copy after login

当然,那句PsyShell::debug()不一定要放在入口文件,它可以放在你一切想要debug的地方例如,我放在默认的首页控制器:

重新运行php内置server并访问页面,然后输入命令:

ls -al
Copy after login

打开PsySH后,输入get_defined_vars()试试?

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

See all articles