Upgrading from php5 to php8 - class constructor problem

I have some projects that probably contain numerous classes where the constructor function is named the same as the class name. In php7 there is a very useful deprecation message when you use one of those. In php8 there is no message, it just doesn’t run the constructor for the class. Is there any way to make php8 alert me to the usage of classes with this problem?

In namespaced classes, or any class as of PHP 8.0.0, a method named the same as the class never has any special meaning.

A method having the same name as the class has no special meaning at all, so, no, there’s nothing php8 will do for you.

I am hoping I used the correct technical jargon, so if I don’t I apologize. The constructor itself can be run as a method. Here’s an example in what I’m talking about.

   public static function fetch_all($sql): array
    {

        $stmt = Database::pdo()->prepare($sql); // Database class <------

        $stmt->execute([ static::$searchItem => static::$searchValue ]);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

So I don’t know why you would want php8 to alert you to usage of the constructor? The purpose of namespaces in the first place is to avoid conflicts with other methods/functions and variables within the classes themselves. That is what makes OOP so interesting is the availability to override methods.

Sponsor our Newsletter | Privacy Policy | Terms of Service