Methods with the same name as their class will not be constructors in a future version of PHP – What’s happened?

When you are developing your WordPress site coming with PHP 7, have you ever got Methods with the same name as their class will not be constructors in a future version of PHP error? If yes, do you want to know how to fix it? In the blog today, we would like to bring you a useful solution for this trouble. Hence, let’s start now!

How to solve the error: methods with the same name as their class will not be constructors in a future version of PHP

The solution for this issue is quite simple. All you need to do is change the name with __construct. By using this way, there won’t be anything affecting the work of your file.

As you know, a constructor is a special function in a class. It is used to allocate the memory to the object with the properties you want to utilize in the class object.

  • In PHP 4: the constructors were the functions with the same name as the class.
  • In PHP 5: you are able to generate a constructor with __construct.
  • From PHP 7: the old style of constructors isn’t accepted and it may be removed in the future version of PHP.

So, now, the warning will appear. However, whenever you use the latest version of PHP, your projects can’t work and there will be an error.

For example, in PHP 4 and 5, you can use the following code:

<?php
namespace Client;
class ClientContact {
public function ClientContact() {

        // treated as constructor in PHP 4 to 5.3.2
        // treated as regular method as of PHP 5.3.3
        // Gives warning in PHP 7.0
}
}
?>

However, in PHP 7.0 or higher, you should use:

<?php
namespace Client;
class ClientContact
{

 public function __constructor()
 {
    //put all your logic to instantiate an object with class properties
 }
}
?>

The name ClientContact will be changed into __constructor in the newest PHP version. Therefore, the warning or error won’t occur.

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

In conclusion, we think that the solution to this trouble may not be complicated. Everything will be solved after a small change in the name of the constructor. So, we hope that you can address the issue without any difficulties. If you have any queries, then don’t forget to show it by leaving your comment below. Last but not least, besides tackling the issue with the PHP version, let’s renew your site with many Free WordPress Themes now.

5/5 - (1 vote)
Lt Digital Team (Content &Amp; Marketing)

Summer Sale Grab 50% Off for everything on today, don't miss it. Coupon code: SUMMER2024 Redeem Now
Summer Sale Grab 50% Off for everything on today, don't miss it. Coupon code: SUMMER2024 Redeem Now