Working on the Stallion Responsive 8.5 update and decided it was time to bite the bullet on fixing some PHP 7 deprecated constructor notices.

These are PHP notices related to some PHP code (from PHP 4 I believe) which the PHP developers plans to deprecate in the future. Right now they aren’t PHP errors, but will be sometime in the future. Currently (October 2016) the PHP code is valid in PHP 5.* and PHP 7.0.*, so this is future proofing Stallion Responsive: IF PHP deprecates the code in PHP 7.1.* (not released) for example, Stallion Responsive 8.5 (update very soon) will be ready.

PHP 7 deprecated Constructor Notices

An example of the PHP 7.0.* notices are these:

PHP 7 Deprecated Constructor Error Notice

PHP 7 Deprecated Constructor Error Notice

Note: The above screenshot is from a localhost server (on my PC) with this added to the wp-config.php file so I see PHP notices and warnings:

define('WP_DEBUG', true);
define('WP_DEBUG_DISPLAY', true);
define('WP_DEBUG_LOG', true);

I develop WordPress themes and WordPress plugins on a development server on a computer, I use WAMPserver 3 (runs under Windows) which allows me to test Stallion Responsive etc… under different server environments (I test under PHP 5.3, PHP 5.4, PHP 5.5, PHP 5.6 and PHP 7.*) on my computer without having to mess around with live sites or live servers (I don’t have a live server running PHP 7.*).

The line in my wp-config.php file “define(‘WP_DEBUG_LOG’, true);” means I have a text file with the PHP notices and errors on my PC under “/wp-content/debug.log” :

[03-Oct-2016 10:32:37 UTC] PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; stallion_gtranslator has a deprecated constructor in ..\wp-content\themes\stallion-responsive\widgets\google-translation.php on line 12

[03-Oct-2016 10:32:37 UTC] PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; st_wg_flickr has a deprecated constructor in ..\wp-content\themes\stallion-responsive\widgets\flickr-widget.php on line 2

[03-Oct-2016 10:34:37 UTC] PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; stallion_caticon_widget has a deprecated constructor in ..\wp-content\themes\stallion-responsive\widgets\stallion_cat_icons.php on line 8

What these relate to are PHP code blocks with a PHP class which contains a PHP function with the same name. This is part of the code from the google-translation.php file (it’s a WordPress widget to translate a webpage on the fly using Google’s translation service).

class stallion_gtranslator extends WP_Widget {
	function stallion_gtranslator() {
		... function code here ...
	}
}

As you can see the PHP class is called “stallion_gtranslator” and there’s a function inside the class with the same name (it’s the “function stallion_gtranslator() {” line that’s the problem.

Note: “class stallion_gtranslator extends WP_Widget {” is line 12 in the PHP notice listed above, but the PHP code problem is really on line 13. Take this into account when hunting for the relevant code, the problem code could be dozens of lines inside the PHP class that’s tripping the deprecated constructor notice.

Fixing PHP 7 deprecated Constructor Notices

It is surprisingly easy to fix, simply replace the function named “function stallion_gtranslator()” with a function named “function __construct()”. See example below.

class stallion_gtranslator extends WP_Widget {
	function __construct() {
		... function code here ...
	}
}

Stallion Responsive is a big WordPress theme, has dozens of features built from loads of PHP plugins, widgets and PHP code snippets I’ve collected over the past decade or so and combined together into one SEO theme package.

Through all the code there was a couple of dozen of the above “PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP;” notices. All of them were fixed with the above method without a single issue: like I said above “surprisingly easy to fix” :-).

I found other ways to fix these issues above on Stackoverflow etc… some of these revolved around renaming the class, which can be problematic when dealing with PHP libraries.

This one looks like it makes sense, add an empty function called “function __construct() {}” above the problem function.

Problem code:

class stallion_gtranslator extends WP_Widget {
	function stallion_gtranslator() {
		... function code here ...
	}
}

Fixed code:

class stallion_gtranslator extends WP_Widget {
	function __construct() {}
	function stallion_gtranslator() {
		... function code here ...
	}
}

I tested this code and it caused the code to break.

David Law

David Law > AKA SEO Dave
*
: Experienced WordPress SEO Expert, Internet Marketer, Developer of the Best SEO Package Stallion Responsive WP SEO Theme (tested to WordPress 4.7 December 2016) and Nice Guy :-)

Website - SEO Tutorial for WordPress