As of PHP 7.1.0 and later, all class-based methods will no longer be able to be invoked as class constructors. Only function constructor calls will be allowed in the future versions of PHP.
This change is part of the move to strict static typing in PHP, where every variable and parameter must have a defined type. Since classes do not have types, all classes will now have to be declared as interfaces, with only concrete classes being allowed as instances.
This change is set to take place in the next major version of PHP, version 8. So as not to break any existing code, this change will only apply to new code written that uses classes as constructors. Code using functions as constructors will not break.
If you find your code using the same name for a class and a method, consider changing the method name so that it is not the same as the class name.
Use class constants

Another feature introduced in PHP 7 is class constants. Constants are defined just like variables, but with the constant keyword. Like static properties and methods, class constants cannot be changed when a class is constructed.
Class constants are also unique in that they cannot be overridden by instances of the class. Once set, its value does not change.
Because of this, it is a good idea to use constants to represent defaults for properties or methods. Then, in case of changes, you can simply update the constant instead of re-doing entire components of the class.
Using classes effectively can help improve your code quality and maintainability, as well as facilitate collaboration on projects.
Use self-reference for instance configuration

A related issue to constructor confusion is the use of methods with the same name as the class as instance configuration methods.
These methods also have a $this keyword prefixed to them, which can lead to confusion about whether to use $this in your instance configuration method or not.
The problem with these instance configuration methods is that they will always return the class itself instead of an instance of the class. As such, they will always return an object of the class itself, not an instance configured by your method.
As of PHP 7.2, these instances have been made constructors so that you can at least configure your object directly instead of having to call another method to do so.
As such, we recommend using self-reference for instance configuration rather than other methods and attributes on the class itself.
Avoid constructors that take arguments

As of PHP 7.2, methods with the same name as the class will not be constructors in a future version of PHP. So, classes that are created with classes that have constructor methods that take arguments will not work when this change occurs.
This is a major change to prevent developers from using constructors that take arguments. Constructors that take no arguments are safe and will continue to work after this change occurs.
Because of this, it is advised to create classes that do not have constructors that take arguments. If you need to set up variables or execute code when creating an instance of a class, do so before calling the constructor using an established set of code or helpers.
Check out the article for more details on this change and how it will affect your code.
Consider using a factory pattern

In order to avoid problems with class and method naming conflicts in the future, you should consider using a factory pattern instead.
A factory pattern is when you define a class that creates other objects for you. These objects can be anything, from databases to models to instances of classes.
For example, instead of having a BaseModel class that is the base for all models, you could have a BaseModelFactory class that generates models for you.
This way, if someone wanted to create a model of type A, they could send your application a model of type A, and your application could generate it for them using the BaseModelFactory class.
Use the namespace feature to group methods by purpose

A related feature you can use to maintain consistency and organization is the namespace feature. Namespaces allow you to group methods by purpose, rather than having all of the methods return the class itself.
For example, let’s say you have a User class with a load() method that loads the user information from the database. However, there is also a setUser() method that sets the user information directly.
In this case, you could create a UserInterface namespace to hold both of these methods so that they are not confused. Then, you could create an interface called Entity that all objects that represent something in your system implement.
This way, everything in your system that represents something has to implement this interface, so all of your entities have access to these two specific methods. Using namespaces to group related methods like this promotes organization and consistency within your codebase.
Use get_called_class() to avoid strict validation errors

Another way to avoid validation errors is to use the get_called_class() method. This method was introduced in PHP 5.6 and is a way to get the class that will be called next.
Since constructors cannot be named the same as their class, using get_called_class() can help you predict what the next called class will be and thus what type of error will be thrown.
Get called class can only be used in situations where there is only one possible return value, so it cannot be used in all situations. It also requires some coding knowledge to know when to use it. For most cases, it is better to just always use it!
It is important to note that get_called_class() will return either Class or string . Since constructors require a defined set of parameters, getting an error saying no parameters matched does not mean there were no parameters provided, it could just mean that there was no constructor provided.
Ensure that class methods are not called as functions

A major warning for class method renaming is that class methods that are called as functions will no longer work. This can be very problematic when working with legacy code, or when working with new features in PHP before they are completely finished.
Since function names are not being changed, only class methods, this issue will crop up every time a new version of PHP is released until this feature is completely finished. As of the time of this writing, it is currently in the beta stage.
If you encounter this issue while working with legacy code or old features of PHP, your only option is to rework how you do things so that there are no more function calls to nonexistent functions.
Make sure to check your code and your work against the new version of PHP to ensure that this does not happen.
Document which class methods are constructors for your users

As of PHP 7.1.0, methods with the same name as their class will no longer be constructors. This change will be in effect in the next major version of PHP, PHP 8.0.
As a result, classes that use similar naming conventions for methods as their classes will need to be modified to have a different method name. Users of your classes will need to call new ConstructorName() in order to create an instance of your class.
It is recommended that you add a comment above your class declaration stating which methods are constructors so that users of your class know how to create an instance of it.
You can also update your classes so that all instances are created using public static constructor syntax. This way, all instances are created using the same method name, making it easier for users to know how to create an instance of your class.


