Today, the accepted answer is out of date.
Renaming classes is bad practice: you must remember what and where to rename each time you upgrade to a newer version. Sometimes (for example, using Reflection or a complex dependency structure) this is not possible without radical refactoring. And this is a random difficulty that you want to avoid. This is why namespaces were added in PHP. Java, C ++ or C # do not use __construct , they use a named constructor, and there is no problem with them.
Starting with PHP 5.3.3, methods with the same name as the last element of the namespaced class name will no longer be construed as a constructor. This change does not affect classes that do not contain names .
Example
namespace Foo; class Test { var $a = 3; function Test($a) { $this->a = $a; } function getA() { return $this->a; } } $test = new Test(4); echo $test->getA();
Note that named constructors are not deprecated (PHP 5.5 today). However, you cannot predict that your class will not be used in the namespace , so __construct should be chosen.
Clarification of the bad practices mentioned above (for Dennis)
Somewhere in your code you can use ReflectionClass :: getName () ; when you rename the class, you need to remember where you used Reflection and check if the result of getName() is still consistent in your application. The more you need to remember something specific, the more likely something is forgotten, which leads to errors in the application.
Parents cannot control all the classes in the world that depend on them. If allow_url_include is enabled, some other websites may use the class from your server, which may result in failure to rename a class. This is even worse in the compiled languages mentioned above: the library can be copied and added to other code.
There is no reason to rename a class:
- If class name conflicts, use namespaces
- if responsibility for a class is shifted, output another class instead
In PHP classes in the namespace, a method with the same name should be avoided: intuitively, it must create an object created by the class; if he does something else, why give him the same name? It must be a constructor and nothing more. The main problem is that the behavior of this method depends on the use of the namespace.
No problem with __construct constructors in PHP. But not the smartest idea to change named constructors.