Home/ Engineering Insights/ Old-Style PHP Constructors Stop Working in PHP 8: How to Find and Fix Them

Old-Style PHP Constructors Stop Working in PHP 8: How to Find and Fix Them

Source. This article is based on PHP manual: Constructors and destructors. Details were checked against it on November 4, 2026; check the source for later changes.

This is one of the quieter PHP 8 breaks. A class written in the PHP 4 style looks like this:

class Mailer
{
    var $host;

    function Mailer($host)
    {
        $this->host = $host;
    }
}

On PHP 5 the method named Mailer ran when you wrote new Mailer('smtp.example.com'). On PHP 8 it does not. There is no error at the point of the problem: the object is created, the constructor body is skipped, and the failure shows up later, somewhere else. This guide explains what changed, how to find every affected class and how to fix it.

What changed

According to the manual's page on constructors and destructors, before PHP 8.0.0 a class in the global namespace treated a method named like the class as an old-style constructor. The syntax was deprecated and raised E_DEPRECATED, but it still worked. If both __construct() and a same-name method existed, __construct() was called. From PHP 8.0.0 a method named like the class "never has any special meaning".

The PHP 7.0 deprecation notes describe the warning: PHP 7 emits E_DEPRECATED when a PHP 4 constructor is the only constructor defined in a class, with the text "Methods with the same name as their class will not be constructors". The PHP 8.0 incompatible changes list the removal.

Why it is hard to spot

Nothing fails where the constructor should have run. Properties it sets stay empty, connections are never opened and defaults are never applied. The symptoms appear elsewhere: a call to a member function on null, an empty list, a setting that is ignored, or the count() TypeError you get when an array was never initialised. If an upgrade produces a scatter of unrelated errors in code you did not touch, old-style constructors are a good suspect.

Option 1: let PHP 7 tell you

If the site can run on PHP 7.x, turn on full error reporting on a staging copy and read the log. Every affected class loaded during your test produces the deprecation message:

grep -rn "Methods with the same name as their class" php-error.log | sort | uniq -c

; php.ini on the staging copy
error_reporting = E_ALL
display_errors = Off
log_errors = On

This only catches classes that are actually loaded while you exercise the site, so an unused admin screen can hide one. Use it together with the script below.

Option 2: scan the code

This short script reads every PHP file with the built-in tokenizer and prints any method whose name matches the class declared above it. It ignores case, because PHP method names are not case sensitive. Save it as find-old-constructors.php and run php find-old-constructors.php /path/to/code.

<?php
// usage: php find-old-constructors.php /path/to/code
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($argv[1]));
foreach ($it as $file) {
    if (substr((string) $file, -4) !== '.php') {
        continue;
    }
    $tokens = token_get_all(file_get_contents($file));
    $class = null;
    $n = count($tokens);
    for ($i = 0; $i < $n; $i++) {
        if (!is_array($tokens[$i])) {
            continue;
        }
        if ($tokens[$i][0] !== T_CLASS && $tokens[$i][0] !== T_FUNCTION) {
            continue;
        }
        $name = null;
        for ($j = $i + 1; $j < $n; $j++) {
            if (is_array($tokens[$j]) && $tokens[$j][0] === T_STRING) {
                $name = $tokens[$j][1];
                break;
            }
            if (!is_array($tokens[$j]) || $tokens[$j][0] !== T_WHITESPACE) {
                break;
            }
        }
        if ($name === null) {
            continue;
        }
        if ($tokens[$i][0] === T_CLASS) {
            $class = $name;
        } elseif ($class !== null && strcasecmp($name, $class) === 0) {
            echo $file . ': ' . $class . '::' . $name . "()\n";
        }
    }
}

We ran it on a small sample file with two old-style constructors and it listed both. It can give false positives: it does not understand namespaces, so it also lists namespaced classes (never affected, per the manual), and a plain function declared after a class could match by name. Check each hit by eye.

The fix

Rename the method to __construct(). Also search for explicit calls to the old name, because child classes written in the same style call the parent by its class name:

// before
class Report extends Base
{
    function Report($title)
    {
        parent::Base($title);
    }
}

// after
class Report extends Base
{
    public function __construct($title)
    {
        parent::__construct($title);
    }
}

If you rename the parent's method but leave parent::Base() in a child, the child breaks, so change both together. While a clean-up is in progress on PHP 7 or older, __construct() wins if both exist, so you can add the new method first and remove the old one once nothing calls it.

Check the result

After the rename, create each fixed class in a short test and assert that the state set up by the constructor is there. These tests are small and the behaviour is easy to state, which makes them a good first target for the approach in adding tests to legacy PHP code. Then run the site on PHP 8 in staging and watch the log. We have not run these changes on your code, so check them on a copy first.

When there are many

A large legacy codebase can hold dozens of these classes, mixed with other removed features. Our legacy system support work scans the code, fixes what the scan finds and tests the result. For the wider picture see what breaks between PHP 5.6 and PHP 8. Related errors from the same upgrade: Call to undefined function mysql_connect() and Passing null to parameter deprecations in PHP 8.1.

Frequently asked questions

Does PHP 8 show an error for old-style constructors?

No. From PHP 8.0.0 the method is an ordinary method, so there is no notice and no error. The constructor code simply does not run. Check the logs on PHP 7.x or scan the code.

Do namespaced classes have this problem?

No. The manual says that in namespaced classes a method named like the class never had special meaning, so they were never treated as constructors.

Can I rename to __construct before I upgrade?

Yes. The __construct() name works on PHP 5, so you can make the change first and test it on your current version.

Hundreds of legacy classes to check?

We scan the codebase, fix the constructors and verify the behaviour before your platform moves to PHP 8.

See our Legacy System Support

Read about our legacy PHP modernisation services →

Ravi Raghav, Founder & CEO of AllScript Lab
Written by Ravi Raghav Founder & CEO