当我按照官方[手册][1]中有关如何安装 PEAR 的说明进行操作时,出现了此错误:
Fatal error: Uncaught Error: Failed opening required 'phar://go-pear.phar/index.php' (include_path='C:\xampp_latest\php\PEAR') in C:\xampp_latest\php\go-pear.phar:1284 Stack trace: #0 {main} thrown in C:\xampp_latest\php\go-pear.phar on line 1284
我尝试寻找其他解决方案并找到了[这个][2]。但是,我仍然无法安装 pear,并且仍然收到此错误:
PHP Fatal error: Array and string offset access syntax with curly braces is no longer supported in C:\xampp_latest\php\go-pear.php on line 1182
.
我尝试通过基于网络和命令行的安装,但得到了同样的错误。
又一个更新.. 我继续进行更多搜索并得到了这个: 链接 因此,我尝试按照错误中的建议将不同文件中的大括号更改为方括号,最后,我收到此错误:
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function error_handler(), 4 passed and exactly 5 expected in C:\xampp_latest\php\pear\pearcmd.php:446
Stack trace:
#0 [internal function]: error_handler(8192, 'trim(): Passing...', 'C:\\xampp_latest...', 152)
#1 C:\xampp_latest\php\pear\PEAR\XMLParser.php(152): trim(NULL)
#2 C:\xampp_latest\php\pear\PEAR\XMLParser.php(166): PEAR_XMLParser->postProcess(NULL, 'options')
#3 [internal function]: PEAR_XMLParser->endHandler(Object(XMLParser), 'options')
#4 C:\xampp_latest\php\pear\PEAR\XMLParser.php(102): xml_parse(Object(XMLParser), '<commands versi...')
#5 C:\xampp_latest\php\pear\PEAR\Command.php(247): PEAR_XMLParser->parse('<commands versi...')
#6 C:\xampp_latest\php\pear\PEAR\Command.php(302): PEAR_Command::registerCommands()
#7 C:\xampp_latest\php\pear\pearcmd.php(54): PEAR_Command::getCommands()
#8 {main}
thrown in C:\xampp_latest\php\pear\pearcmd.php on line 446
[1]: https://pear.php.net/manual/en/installation.getting.php
[2]: https://www.ivankristianto.com/install-or-update-pear-on-xampp-for-windows/
基本上,xampp 提供的 PEAR 并未更新为在 PHP 8.x 下运行。并面临 PHP 8.0 中多个已弃用和删除的功能,这些功能会导致 PHP 致命错误。
1)访问字符问题
第一个问题是,当使用大括号 {}
进行访问时,
通过从零开始的偏移量进行字符串访问已被删除,并且只能使用方括号
[]
。
PHP 7.4 中已弃用使用 {} 语法访问字符串文字中的字符。这已在 PHP 8.0 中删除。
对比原代码
$arg{0}
有固定代码:
$arg[0]
解决方案:
使用正则表达式
xampp/php/pear
搜索 \{(\$[a-zA-Z0-9\+]*)\}
文件夹中的所有文件,并将其替换为 [$1]
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function error_handler(), 4 passed and exactly 5 expected
。
解决方案:
搜索
set_error_handler(
调用并找到引用的回调函数 error_handler
并将最后一个参数设为可选。 xampp\php\pear\pearcmd.php
并且函数定义位于第 446 行:
对比原函数定义:
function error_handler($errno, $errmsg, $file, $line, $vars)
应用修复后:
function error_handler($errno, $errmsg, $file, $line, $vars = null)
注意:我发现“bug”早在 2021 年 9 月就已经在 Apache 朋友支持论坛 上报告了。
3)未定义函数each()问题
第三个问题是删除了 PHP 函数 each(),该函数抛出
PHP Fatal error: Uncaught Error: Call to undefined function each()
。
此函数从 PHP 7.2.0 开始已弃用,从 PHP 8.0.0 开始已删除。强烈建议不要依赖此功能。
解决方案
搜索所有出现的
each(
(使用空格消除结果集中的函数“foreach”),并使用函数 foreach
以及每个文件中使用的正确参数来检查和更新它。 \
while
语法示例
while (list($i, $arg) = each($args)) {
可以替换为
foreach ($args as $i => $arg) {
list
语法示例
list(,$first) = each($lines);
可以替换为
foreach($lines as $first){}
If - else
语句中使用了一些其他情况,可以用emtpy($args)
替换,然后用foreach($args as $opt_arg){}
来构建变量$opt_arg。
If - else
语法示例
if (!strlen($opt_arg) && !(list(, $opt_arg) = each($args))) {
可以替换为
if (!strlen($opt_arg) && !empty($args)) {
foreach($args as $opt_arg){}
PEAR 终于可以使用 XAMPP 版本:8.2.0
C:\xampp\php>pear help version
PEAR Version: 1.10.1
PHP Version: 8.2.0
Zend Engine Version: 4.2.0
Running on: Windows NT D5KGFJF 10.0 build 19045 (Windows 10) AMD64