Use of deprecated PHP4 style class constructor is

2020-02-02 03:29发布

问题:

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

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

How do I fix that?

回答1:

Change the function to:

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 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.

The error example, as per the documentation:

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