__construct () vs SameAsClassName () for constructor in PHP

Is there any advantage to using __construct() instead of the class name for the constructor in PHP?

Example:

 class Foo { function __construct(){ //do stuff } } 

OR

 class Foo { function Foo(){ //do stuff } } 
+64
constructor php php-7
Oct 20 '08 at 5:52
source share
11 answers

I agree with gizmo, the advantage is that you do not need to rename it if you rename your class. DRY.

Similarly, if you have a child class, you can call

 parent::__construct() 

to invoke the parent constructor. If down the track you change the class inherited by the child class, you do not need to change the call of the construct to the parent.

This seems like a small thing, but not changing the name of the constructor call for your parents' classes can create subtle (and not very subtle) errors.

For example, if you inserted a class into your heirachy but forgot to change the constructor calls, you could start calling grandparents constructors instead of parents. This can often lead to undesirable results, which can be difficult to notice.

Also note that

As with PHP 5.3.3, methods with the same name as the last element of the namespace name will no longer be construed as a constructor. This change does not affect classes that do not contain names.

Source: http://php.net/manual/en/language.oop5.decon.php

+63
Oct. 20 '08 at 9:25
source share

__construct was introduced in PHP5. This is how you should do it now. However, I do not know any advantages as such.

From the PHP manual:

For backward compatibility, if PHP 5 cannot find the __construct () function for this class, it will look for the old-style constructor function by the name of the class. In fact, this means that the only case that would have compatibility issues is that the class had a method called __construct () that was used for different semantics

If you are in PHP5, I would recommend using __construct to avoid PHP from appearing elsewhere.

+18
Oct. 20 '08 at 5:57
source share

The main advantage that I see for __construct is that you do not need to rename your constructor if you change the class name.

+13
Oct 20 '08 at 6:01
source share

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(); // 3, Test is not a constructor, just ordinary function 

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.

+10
Jun 16 '13 at 9:14
source share

The best advantage of using __contruct() instead of ClassName() is the extension of the classes. It is much easier to call parent::__construct() instead of parent::ClassName() , as it is reused among classes, and the parent element can be easily changed.

+7
Oct. 20 '08 at 9:30
source share

In your example, Foo::Foo sometimes called the constructor of PHP 4 or the old style, since it comes from the days of PHP 4:

 class Foo { // PHP 4 constructor function Foo(){ //do stuff } } 

PHP 4 constructors will be deprecated, but not deleted in PHP 7. They will no longer be construed as constructors in any situation in PHP 8. Future compatibility is certainly a big reason not to use this function.

+7
May 13 '15 at 3:03
source share

In PHP 5, the advantage would be that performance would be better. First, he will look for a constructor named __construct , and if he does not find it, he will look for constructors named className . Therefore, if he finds a constructor named __construct , he does not need to search for a constructor named className .

+3
Oct 20 '08 at 6:00
source share

Future compatibility. There is always a chance that the previous code remaining in the language for backward compatibility will be removed in a future version.

+2
Oct 20 '08 at 9:14
source share

Well, several years have passed since this question was asked, but I think I should answer this question because everything has changed, and for readers in the future I want the information to be updated!


So in php-7 they remove the parameter to create the constructor as a function with the same name as the class. If you still do this, you will receive E_DEPRECATED .

You can learn more about this proposal (proposal accepted) here: https://wiki.php.net/rfc/remove_php4_constructors

And a quote from there:

PHP 7 will generate E_DEPRECATED if the PHP 4 constructor is defined. When the method name matches the class name, the class is not in the namespace, and the PHP 5 constructor (__construct) is missing, then E_DEPRECATED will be emitted. PHP 8 will no longer emit E_DEPRECATED, and methods will not be recognized as constructors.

Also you will not get E_STRICT in php-7 if you define a method with the same name as the AND a __construct() class.

You can also see it here:

PHP 7 will also stop E_STRICT when there is a method with the same name as the class and __construct.


Therefore, I would recommend that you use __construct() , since in the future you will have less problems with this.

+2
Mar 29 '15 at 13:50
source share

If the __construct and SameAsClassName methods exist, __construct will be executed, the SameAsClassName method will be skipped.

+1
Dec 6 '13 at 6:35
source share

I think the main reason is that this is a language convention. You do not need to force the language to act like someone else.

I mean, in Objective-C, for example, you prefix constructors with -init. You can create your own constructor using the name of your class, but why? Is there a reason to use this scheme instead of a language convention?

0
Jun 06 '13 at 0:44
source share



All Articles