怎么在VS Code编辑器玩转PHP开发
这是一份VS Code轻量级PHP开发环境配置方案,通过在VS Code中安装PHP开发相关插件,实现代码格式化、自动补全代码、类方法定位、php调试、预览与历史版本找回扥功能以满足良好的php开发环境,希望本文对大家有所帮助。
在使用该方案前,提前配置好PHP运行环境,php版本至少7以上,当前方案PHP版本8。
预先配置php.exe可执行文件路径

配置并保存:
{
"php.validate.executablePath": "E:/php/env/php-8.0.3/php.exe",
"php.executablePath": "E:/php/env/php-8.0.3/php.exe"
}
实现代码格式化、提示、补全、类方法定位等
(1. 安装PHP Intelephense智能感知插件

(2.配置PHP Intelephense

(生成配置,有需要自定义内容)

(生成配置,有需要自定义内容)
(3.效果

常用快捷键:
代码格式化:
Mac: shift + option + f ; Windows:shift + alt + f
类或方法方法定位:
Mac:command + 鼠标左健 ;Windows:ctrl + 鼠标左键
返回到上次跳转位置:
Mac:control + “-” ;Windows:alt + 左箭头
快速类、函数注释
(1. 安装php-docblocker

(2. 简单配置php-docblocker,添加默认作者、邮箱,类注释加个作者



(3. 使用:在类或方法上面输入/**换行即可

支持命名空间解析器、引入类
(1. 安装PHP Namespace Resolver

(2. 引入类快捷键,按ctrl+alt+i。
Debug x3调试兼预览PHP运行结果
(1. 先下载安装php的Xdebug扩展库(php线程版下ts版的),下载地址:
https://xdebug.org/download

(2. 将扩展放到php指定扩展库目录

(3. 修改php配置文件php.ini,末行新增如下debug配置(根据需要配置),后重启web服务(apache等)
php.ini配置参考:
https://xdebug.org/docs/step_debug
所有配置说明:
https://xdebug.org/docs/all_settings#mode

xdebug2x与3x配置不同

php.ini xdebug3x配置
[xdebug]
;xdebug3扩展
zend_extension = "E:\php\env\php-8.0.3\ext\php_xdebug-3.0.3-8.0-vs16-x86_64.dll"
;xdebug3与xdebug2配置不同,配置参考https://xdebug.org/docs/step_debug#start_with_request
;设置debug模式
xdebug.mode=debug
;激活debug
xdebug.start_with_request=yes
;允许不同机器访问
;xdebug.discover_client_host = false
;远程主机 默认localhost
;xdebug.client_host=localhost
#远程端口
xdebug.client_port = 9000

phpinfo查看xdebug安装成功状态
(4. Vscode 应用商店搜索安装“php debug”

(5. 创建lunch.json文件,弹出语言列表选择php,配置内容中只需确保端口无误,其他默认配置就好


(6. 添加断点,启动并可进入调试状态

说明:利用“php debug”插件便可以预览运行结果或直接在终端访问“php 文件名”,若想运行更多编程语言,推荐插件“Code Runner”,运行页面效果直接浏览器访问。
跳转浏览器访问可执行页面
(1. 安装PHP Server

(2. 配置:不配置默认也行,配置一下端口与php.ini,端口配置80或与我们习惯的端口其他一致的也行

(3. 预览操作

历史版本找回与恢复
安装Local history插件,尽可能避免丢失风险


其他插件推荐
版本控制:svn、git(git已集成查阅怎么使用即可)
Php框架支持:thinkphp系列(thinkphp6 snippets等)、laravel系列(Laravel 5 Snippets等)
说明
若以上配置了未生效,尝试重启VS Code工具,一般实时生效。