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
安装完毕后,PsySH已经安装到/Users/{用户名}/.composer/vendor/psy/psysh目录下
此时,你可以直接运行:
$ /Users/{用户名}/.composer/vendor/psy/psysh/bin/psysh
为了使用方便,建议将它加入到环境变量:
$ echo 'export PATH="/Users/{用户名}/.composer/vendor/psy/psysh/bin:$PATH"' >> ~/.bashrc$ source ~/.bashrc
Windows
同样地,使用Composer安装,按win键+R键,输入cmd,打开windows控制台,然后:
composer global require psy/psysh
安装完成后,PsySH被安装到C:Users{用户名}AppDataRoamingComposervendorpsypsysh
因为bin/psysh文件并不是windows的可执行文件,所以需要使用以下命令运行PsySH
php C:Users{用户名}AppDataRoamingComposervendorpsypsyshbinpsysh
为了使用方便,在C:Users{用户名}AppDataRoamingComposervendorpsypsyshbin目录下新建一个名为psysh.bat的文件,其内容如下:
<a href="http://www.jobbole.com/members/echo">@ECHO</a> OFFphp "%~dp0psysh" %*
此时,把C:Users{用户名}AppDataRoamingComposervendorpsypsyshbin加入到系统的环境变量PATH,以后可以直接在cmd下运行psysh了:
C:UsersVergil>psyshPsy Shell v0.6.1 (PHP 5.6.8 — cli) by Justin Hileman>>>
特性
上面说过,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
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
然后即可在浏览器访问你的应用: http://localhost:8080
你将会看到以下画面,PsySH启动了
其他操作,和CLI操作差不多。例如:
ls -al ThinkModel
show \Think\Model::find
doc \Think\Model::save
当然,那句PsyShell::debug()不一定要放在入口文件,它可以放在你一切想要debug的地方例如,我放在默认的首页控制器:
重新运行php内置server并访问页面,然后输入命令:
ls -al
打开PsySH后,输入get_defined_vars()试试?

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
