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

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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 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

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply