神技能? PHP7旧代码如何转换成新代码以适应 php8+ 跨版本PHP代码转换终极教程?任何PHP5.3+代码的即时升级和即时重构 ?如何基于PHP 8.0开发适配低版本PHP代码?
看看这些标题 真是神了‘
我们就像有了高纬度的神力 可以随意在php 高低版本之间转换代码? 是的
今天我遇到了难题 : 有超火的两个源码 wordpress和 discuz 都是最新版本的
源码可完美适配 php8.1最新版本 但是,并不是所有的模板和插件都兼容php8+
如果在模板插件的作者不更新版本前提下 ,我们如何自己修改,如果是程序猿 这太好办了 ,只要根据buglog 一步步 一个一个代码修改便是
但是现在我不是程序员 还想修改 怎么办? 今天我用了一天的时间去全网 *(谷歌+百度)搜索了相关内容 找到这么几篇文章 来学习
跨版本PHP代码转换终极教程
https://www.wbolt.com/transpiling-php.html
PHP版本升级了,旧代码怎么办?告诉你一个快速迁移的好方法
https://baijiahao.baidu.com/s?id=1685033852508849987
Rector: 任何PHP5.3+代码的即时升级和即时重构
https://www.5axxw.com/wiki/content/7dhzju
如何基于PHP 8.0开发适配低版本PHP代码
https://zhuanlan.zhihu.com/p/420255968
里面有一个共同点 就是安装 Rector 用命令转换PHP
Rector库包含PHP8.0到7.1范围内的大多数代码转换规则
这是精髓: 步骤
如何转换PHP(Transpile PHP)
首先,我们需要在开发项目中安装Rector:
composer require rector/rector --dev
然后,我们在项目的根目录中创建一个rector.php
配置文件,其中包含所需的规则集。要将代码从PHP 8.0降级到7.1,我们使用以下配置:
use Rector\Set\ValueObject\DowngradeSetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(DowngradeSetList::PHP_80);
$containerConfigurator->import(DowngradeSetList::PHP_74);
$containerConfigurator->import(DowngradeSetList::PHP_73);
$containerConfigurator->import(DowngradeSetList::PHP_72);
};
为了确保流程按预期执行,我们可以在dry mode下运行Rector的process
命令,传递要处理的位置(在本例中,是src/
文件夹下的所有文件):
vendor/bin/rector process src --dry-run
要执行转换,我们运行Rector的process
命令,该命令将修改现有位置内的文件:
vendor/bin/rector process src
请注意:如果我们在开发计算机中运行rector process
,源代码将在src/
下进行转换。但是,我们希望在不同的位置生成转换后的代码,以便在降级代码时不会覆盖源代码。因此,在持续集成期间运行流程最合适。
只可惜 本人光安装composer和 Rector就花费了巨大时间 ,因为并没有详细的步骤来说明 需要自己去折腾
我所用的环境 是CentOS 7.9 安装了BT宝塔底层,安装了 nginx1.22+php7.4 后来从php8.1及php8.0之间来回切换
执行命令出现大量错误提示 都是百度一步步解决了
我先说明下安装底层系统和bt宝塔 这个非常简单 VMware虚拟机安装centos 这个不说了 是基础 不会的百度自己找教程
BT宝塔 在ssh下一步安装 yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh 进入面板后续绑定账户 没有的先去官网注册
如果不想绑定账号 可以使用宝塔开心版 这个已经非常成熟 博主已用于生产环境 无风险点击获取
然后是安装nginx1.22+php8.0 这里推荐直接8.0吧 因为 8.1不会自动装composer
在看上面四篇文章前 我是研究了 phpstan这个工具 看官网 https://phpstan.org/ github https://github.com/phpstan/phpstan
这个东西干嘛的 就是检测php代码的错误 然后有针对性的改正代码 官网不需要任何搭建 可以直接在线用 https://phpstan.org/try
但是也有一个搭建环境 只有这么一个说明页面 https://phpstan.org/user-guide/getting-started
里面的安装 composer
就是一句命令
To start performing analysis on your code, require PHPStan in Composer:
composer require --dev phpstan/phpstan
Composer will install PHPStan’s executable in its bin-dir
which defaults to vendor/bin
.
You can also download the latest PHAR and just use that. But without Composer, you won’t be able to install and use PHPStan extensions.
Head here if you want to use PHPStan in Docker.
First run
To let PHPStan analyse your codebase, you have to use the analyse
command and point it to the right directories.
So, for example if you have your classes in directories src
and tests
, you can run PHPStan like this:
vendor/bin/phpstan analyse src tests
You should only analyse files with the code you’ve written yourself. There’s no need to analyse the vendor
directory with 3rd party dependencies because it’s not in your power to fix all the mistakes made by the developers you don’t work with directly.
Yes, PHPStan needs to know about all the classes, interfaces, traits, and functions your code uses, but that’s achieved through discovering symbols, not by including the files in the analysis.
但是会遇到非常多的问题 可参考这篇linux(ubuntu 21.10): 安装composer 2.2.0(php 8.1.1)
我自己折腾的经验是 直接安装php8.0吧 因为 8.1不会自动装composer php8.0会自动调用composer
在BT宝塔面板里 在网站一栏 随便新建一个网站 然后点开 属性 找到 composer 然后点升级composer就会升级到最新版 目前是2.4.2
其实没什么鸟用
去ssh里面执行命令 正确无误的结果 如下
1只要你把要检测的源码文件放到 root\src 跟 tests 文件夹里面 如果没有就新建一个
然后SSH 执行命令vendor/bin/phpstan analyse src tests
如果有错 就会提现
但我们又转回来了 如果是程序员 这太简单了 只需要一个个改就行了
但是不是程序员 又想着改代码 所以回到了 文章开头的 Rector 找到的四篇文章 也都精读了好几遍
无奈 这里面有好几个问题 都一一解决了
最后 执行命令时候 总是提示
Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator
not found 现在仍未解决