“Methods with the same name as their class will not be constructors”: what happens on PHP 8
Short answer
On PHP 7 this was a deprecation notice about very old plugin code: a class whose setup method has the same name as the class (class Slider { function Slider() {…} }). On PHP 8.0 and later that method is simply not run when the object is created, and there is no message about it. The object stays half set up, and the plugin fails later with an error that seems unrelated, such as a count() TypeError. The fix is to rename the method to __construct(). Update or replace the plugin.
The message on PHP 7
A slider class sets up its slides in a method called Slider(), the PHP 4 way of writing a constructor. PHP 7.4 still runs it as a constructor, with a notice; PHP 8.0 doesn’t:
wp-content/plugins/old-slider/includes/class-slider.php
<?php
class Slider {
private $slides;
public function Slider() {
$this->slides = ['welcome.jpg', 'team.jpg'];
}
public function count_slides() {
return count($this->slides);
}
}
$slider = new Slider();
echo 'Slides: ', $slider->count_slides(), "\n";
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Slider has a deprecated constructor in /var/www/html/wp-content/plugins/old-slider/includes/class-slider.php on line 2
Slides: 2Slides:
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in /var/www/html/wp-content/plugins/old-slider/includes/class-slider.php:10
Stack trace:
#0 /var/www/html/wp-content/plugins/old-slider/includes/class-slider.php(15): Slider->count_slides()
#1 {main}
thrown in /var/www/html/wp-content/plugins/old-slider/includes/class-slider.php on line 10On PHP 7.4 the notice names the class. On PHP 8.0 there is no notice about the constructor at all: the error is about count(), because $this->slides was never filled.
php.net, for PHP 8.0: “Methods with the same name as the class are no longer interpreted as constructors. The __construct() method should be used instead.” (php.net)
Why it’s confusing
The real cause, a constructor that no longer runs, produces no message on PHP 8. What you see is a consequence somewhere else: a count() error, an “Attempt to read property … on null” warning, an empty widget. Searching for that message leads you to the wrong place. If the plugin is very old and the error is about a value that “should” be set, this is worth checking.
Fix it, in order
- Site down? Switch PHP back, or pause the plugin through the link in WordPress’s email: the critical error guide.
- Find the plugin from the path in the error message.
- Update it, or replace it: code written this way hasn’t been maintained for a long time.
- Custom code? The developer renames the method to
__construct()(below).
Found before you switch
Because PHP 8 itself stays silent, a check that reads the code is the reliable way to find it:
Other PHP 8.0 changes that hit the same old plugins: create_function(), each(), curly-brace offsets.
Key takeaways
- PHP 7 showed a deprecation notice; PHP 8.0 silently stops calling the method as a constructor.
- The error you see on PHP 8 is a different one, later in the code, because the object was never set up.
- The fix: rename the method to
__construct(). - Code with this pattern was written for PHP 4 or 5: expect other PHP 8 problems in the same plugin.
- A check that reads code finds it before you upgrade, even though PHP 8 itself says nothing.
Frequently asked questions
Why is the error on PHP 8 about something else?
Because the constructor’s work never happened: properties it should have filled are empty, and the first code that relies on them fails, often with a TypeError or a warning about null.
Is there an update for such old plugins?
Sometimes. If the plugin still has this pattern, it hasn’t been updated for years; replacing it is often the better choice.
Check your own site before you upgrade
CompatNav is a free WordPress plugin. It reads the code of your plugins and themes on your own server and tells you, in plain words, what will break and what will only show notices on the PHP version you choose. It never changes your code. It can’t see problems that only appear while code runs with real data, so a quick check of your site after the upgrade still matters.
Sources
About the code examples: each output is the real output of the code shown, run with the official PHP builds, without a php.ini, with all errors reported and displayed. Only the file path was replaced by a neutral server path.