自 PHP 7 起不支持使用已弃用的 PHP4 样式 class 构造函数
Use of deprecated PHP4 style class constructor is not supported since PHP 7
我正在尝试升级我在 SiteGround 上托管的 WP 网站的 PHP 版本。升级工具显示此错误:
33 | WARNING | Use of deprecated PHP4 style class constructor is not
supported since PHP 7
这是我在给定位置找到的代码:
function gc_XmlBuilder($indent = ' ') {
$this->indent = $indent;
$this->xml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
}
我该如何解决?
将函数更改为:
function __construct($indent = ' ') {
$this->indent = $indent;
$this->xml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
}
因为您过去可以通过 class 名称定义构造函数,并且自 PHP 7 起已弃用:
PHP 4 style constructors (methods that have the same name as the class they are defined in) are deprecated, and will be removed in the future. PHP 7 will emit E_DEPRECATED if a PHP 4 constructor is the only constructor defined within a class. Classes that implement a __construct() method are unaffected.
错误示例,根据文档:
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; foo has a deprecated constructor in example.php on line 3
我正在尝试升级我在 SiteGround 上托管的 WP 网站的 PHP 版本。升级工具显示此错误:
33 | WARNING | Use of deprecated PHP4 style class constructor is not supported since PHP 7
这是我在给定位置找到的代码:
function gc_XmlBuilder($indent = ' ') {
$this->indent = $indent;
$this->xml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
}
我该如何解决?
将函数更改为:
function __construct($indent = ' ') {
$this->indent = $indent;
$this->xml = '<?xml version="1.0" encoding="utf-8"?>'."\n";
}
因为您过去可以通过 class 名称定义构造函数,并且自 PHP 7 起已弃用:
PHP 4 style constructors (methods that have the same name as the class they are defined in) are deprecated, and will be removed in the future. PHP 7 will emit E_DEPRECATED if a PHP 4 constructor is the only constructor defined within a class. Classes that implement a __construct() method are unaffected.
错误示例,根据文档:
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; foo has a deprecated constructor in example.php on line 3