php7/pear 需要文件时显示解析错误
php7/pear showing parse error when requiring files
我有一个遗留网站(不是我写的),过去几年一直在服务器上 php5。我正在创建一个带有 php7 的新服务器并测试哪些有效,哪些损坏。
站点通过包含文件 pear/lib/DB.php
使用 pear。我创建了一个只有代码
的全新页面
<?php
require_once( "DB.php" );
?>
这显示了与完整站点完全相同的错误。
出现的错误是
PHP Parse error: syntax error, unexpected 'new' (T_NEW) in /local/sites/php/pear/lib/DB.php on line 310
该站点只需要 DB.php 因为我在 include_path
中的 php.ini 添加了 Pear
检查 Pear 的版本会得到以下信息
$梨版
PEAR Version: 1.10.3
PHP Version: 7.0.15-0ubuntu0.16.04.4
Zend Engine Version: 3.0.0
Running on: Linux cdc-migration-0d 3.13.0-103-generic #150-Ubuntu SMP Thu Nov 24 10:34:17 UTC 2016 x86_64
根据我的研究,它表明最新版本的 Pear 是 php7 兼容的,因此它们应该可以协同工作。知道为什么仅在测试页上要求 DB.php
会立即产生解析错误吗?
编辑:
pear 文件中产生错误的代码如下
function &factory($type, $options = false)
{
if (!is_array($options)) {
$options = array('persistent' => $options);
}
if (isset($options['debug']) && $options['debug'] >= 2) {
// expose php errors with sufficient debug level
include_once "DB/{$type}.php";
} else {
@include_once "DB/{$type}.php";
}
$classname = "DB_${type}";
if (!class_exists($classname)) {
$tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
"Unable to include the DB/{$type}.php file",
'DB_Error', true);
return $tmp;
}
@$obj =& new $classname; // ##### this is line 310 that generates the error #####
foreach ($options as $option => $value) {
$test = $obj->setOption($option, $value);
if (DB::isError($test)) {
return $test;
}
}
return $obj;
}
@$obj =& new $classname;
从 PHP 5.3 开始,不推荐通过引用分配 new 的 return 值。 http://php.net/manual/en/migration53.deprecated.php
这是PHP4种写法PHP.
改为:
$obj = new $classname;
这已从 PHP7 开始删除。
参见:http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.other.new-by-ref
我有一个遗留网站(不是我写的),过去几年一直在服务器上 php5。我正在创建一个带有 php7 的新服务器并测试哪些有效,哪些损坏。
站点通过包含文件 pear/lib/DB.php
使用 pear。我创建了一个只有代码
<?php
require_once( "DB.php" );
?>
这显示了与完整站点完全相同的错误。
出现的错误是
PHP Parse error: syntax error, unexpected 'new' (T_NEW) in /local/sites/php/pear/lib/DB.php on line 310
该站点只需要 DB.php 因为我在 include_path
检查 Pear 的版本会得到以下信息 $梨版
PEAR Version: 1.10.3
PHP Version: 7.0.15-0ubuntu0.16.04.4
Zend Engine Version: 3.0.0
Running on: Linux cdc-migration-0d 3.13.0-103-generic #150-Ubuntu SMP Thu Nov 24 10:34:17 UTC 2016 x86_64
根据我的研究,它表明最新版本的 Pear 是 php7 兼容的,因此它们应该可以协同工作。知道为什么仅在测试页上要求 DB.php
会立即产生解析错误吗?
编辑: pear 文件中产生错误的代码如下
function &factory($type, $options = false)
{
if (!is_array($options)) {
$options = array('persistent' => $options);
}
if (isset($options['debug']) && $options['debug'] >= 2) {
// expose php errors with sufficient debug level
include_once "DB/{$type}.php";
} else {
@include_once "DB/{$type}.php";
}
$classname = "DB_${type}";
if (!class_exists($classname)) {
$tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
"Unable to include the DB/{$type}.php file",
'DB_Error', true);
return $tmp;
}
@$obj =& new $classname; // ##### this is line 310 that generates the error #####
foreach ($options as $option => $value) {
$test = $obj->setOption($option, $value);
if (DB::isError($test)) {
return $test;
}
}
return $obj;
}
@$obj =& new $classname;
从 PHP 5.3 开始,不推荐通过引用分配 new 的 return 值。 http://php.net/manual/en/migration53.deprecated.php
这是PHP4种写法PHP.
改为:
$obj = new $classname;
这已从 PHP7 开始删除。
参见:http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.other.new-by-ref