自PHP 7起,不支持使用不推荐使用的PHP4样式类构造函数 [英] Use of deprecated PHP4 style class constructor is not supported since PHP 7

查看:329
本文介绍了自PHP 7起,不支持使用不推荐使用的PHP4样式类构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试升级SiteGround上托管的WP网站的PHP版本.升级程序工具显示此错误:

I am trying to upgrade the PHP version of my WP site which is hosted on SiteGround. Upgrader tool shows this error:

33 |警告不推荐使用不推荐使用的PHP4样式类构造函数 自PHP 7起受支持

33 | WARNING | Use of deprecated PHP4 style class constructor is not supported since PHP 7

这是我在给定位置找到的代码:

This is the code I found at the given location:

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";
}

您曾经能够通过类名定义构造函数,而自

As you used to be able to define constructors via the class name and that has been deprecated as of PHP 7:

不推荐使用PHP 4样式构造函数(与在其定义的类中具有相同名称的方法),并将在以后删除.如果PHP 4构造函数是类中唯一定义的构造函数,则PHP 7将发出E_DEPRECATED.实现__construct()方法的类不受影响.

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.

错误示例,根据文档:

已弃用:与类同名的方法在将来的PHP版本中将不再是构造函数; foo在第3行的example.php中有一个已弃用的构造函数

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

这篇关于自PHP 7起,不支持使用不推荐使用的PHP4样式类构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆