Benjamin Crozat The web developer life

Methods with the same name as their class will not be constructors in a future version of PHP

Benjamin Crozat Benjamin Crozat1 minute read

Before you start reading this article, did you know 76 persons subscribed to my newsletter?

Join them and enjoy free content about the art of crafting websites!

Methods with the same name as their class will not be constructors in a future version of PHP

This warning message occurs because class constructors can’t have the same name as their class. You can fix this by changing it to __construct().

  1. Grab your favorite code editor and search for class definitions across your project;
  2. Check for constructor methods with the same name as the class and change it to __construct.

Your modifications should look like this:

class Foo
{
- public function Foo()
+ public function __construct()
{
}
}

That’s it, it’s as simple as that.

But did you know the story behind this change?

In PHP 4, as you know, a constructor was declared with the same name as its class. It was still working in PHP 5, was deprecated in PHP 7.0, and removed in PHP 8.0. That is why you must rename your constructors before migrating to version 8 or greater.

For posterity, you can read more about it on the official PHP documentation: PHP deprecated features in version 7.0.x

You can also see the PHP RFC that led to this: PHP RFC: Remove PHP 4 Constructors

Other posts to read

Let's review some quick wins that Laravel Collections provide to instantly make your codebase better.

Whether PHP is dead or not is a matter of numbers, not opinions. I will provide you with essential statistics that will lead you to make better decisions.

Are you looking for the best web hosting for Laravel projects and all budgets? I've got you covered. Here are the best I found.

AI is a trending topic in the programming space. It enables developers to do incredible things, and many startups build products around it.

Learn why the "Using $this when not in object context" error happens, and let me show you the only way to fix.

Job interviews are not as tough as they seem. I will give you guidance through my past experiences and a list of easy questions that will make you stand out.

When in doubt, clear the cache. In this article, you'll learn about how to clear every cache Laravel uses.

PHP 8.3 will be released in November 2023, and as usual, you need to be up to date with new features and breaking changes for easier transitions.

Redirects in PHP are simple. I will guide you step by step and show you how to dodge some traps. Finally, we'll learn the nuance between 301 and 302 redirects.

Laravel 10 will be released on February 7, 2023. Its development is still ongoing. Let's dive into every relevant new feature we know about already.