__construct() vs SameAsClassName() for constructor in PHP

Viewed 20283

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

Example (__construct):

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

Example (named):

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

Having the __construct method (first example) is possible since PHP 5.

Having a method with the same name as the class as constructor (second example) is possible from PHP version 4 until version 7.

11 Answers

I agree with gizmo, the advantage is so you don't have to rename it if you rename your class. DRY.

Similarly, if you have a child class you can call

parent::__construct()

to call the parent constructor. If further down the track you change the class the child class inherits from, you don't have to change the construct call to the parent.

It seems like a small thing, but missing changing the constructor call name to your parents classes could create subtle (and not so subtle) bugs.

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

Also note that

As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name will no longer be treated as constructor. This change doesn't affect non-namespaced classes.

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

__construct was introduced in PHP5. It is the way you are supposed to do it now. I am not aware of any advantages per se, though.

From the PHP manual:

For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics

If you're on PHP5 I would recommend using __construct to avoid making PHP look elsewhere.

The main advantage I see for __construct, is that you don't have to rename your constructor if you change your class name.

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

In PHP 5 the advantage would be that performance would be better. It will look for a constructor by the name of __construct first and if it doesn't find that, it will look for constructors by the name of className. So if it finds a constructor by the name __construct it does not need to search for a constructor by the name className.

Forward compatibility. There's always a chance that legacy code that's left in the language for backwards compatibility's sake will be removed in a future version.

If there is methods __construct and SameAsClassName method then __construct will be executed, SameAsClassName method will be skipped.

Related