Fixing PHP4 Constructor for PHP7

PHP 7 deprecates PHP4-style constructors. In PHP4, class constructor methods had the same name as the class: ​

This was bad when switching base classes; you did not only have to change the class' extends declaration but also calls to the parent constructor. PHP5 then introduced the generic __construct method name for class constructors, which solved the problem. ​

PHP7 will output a deprecation message when a class with a PHP4-style constructor is loaded (not necessarily used): ​

PHP Deprecated: Methods with the same name as their class will not be
constructors in a future version of PHP;
The SubClass has a deprecated constructor in /path/to/file.php on line 15​

Fixing by renaming​

So you're maintaining code - maybe a library - that has such an old-style constructor: ​

Note that this code works well, even in PHP7. PHP7 logs the deprecation message, but the code still works. ​

Users of your library report the deprecation notice, and you decide to bring the code up-to-date with PHP7 by renaming the constructor method: ​

You release a new version, and suddenly applications fatally break: ​

PHP Fatal error: Call to undefined method SubClass::OldClass()
in /path/to/file.php on line 14​
This is because other applications rely on the existence of the old constructor: ​

A real fix​

To not break everyone using your library, you have to keep backward compatibility: Add the PHP5-style constructor, but keep the PHP4-style one. ​

With that change, your code will not emit deprecation messages anymore while keeping compatibility with older applications: ​

Constructor parameters​

You may need to pass constructor parameters, too.

Notes

PHP Strict Standards: Redefining already defined a constructor for class​
This happens if you put the PHP4 constructor before __construct. The warning will not be raised when __construct is defined first. ​