18

Possible Duplicate:
what is the function __construct used for?

is there any difference between __construct function and function with same name as class has?

class foo {
    function foo ($something){
        echo "I see ".$something." argument";
    }
}

class bar {
    function __construct ($something){
        echo "<br />
            I see ".$something." argument again";
    }
}

$foo = new foo("foo");
$bar = new bar("bar");
Community
  • 1
  • 1
genesis
  • 50,477
  • 20
  • 96
  • 125
  • 5
    See the accepted answer on this question http://stackoverflow.com/questions/455910/what-is-the-function-construct-used-for – AllisonC Jul 29 '11 at 12:17
  • Thank you Allison. People that mark questions as duplicate should be required to post the original question. – Dan Nov 02 '16 at 15:23

5 Answers5

36

The method named is the PHP4 way of doing a constructor.

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.

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.

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

Community
  • 1
  • 1
Damien
  • 5,872
  • 2
  • 29
  • 35
  • Out of curiosity, I'd dare a "why"... why did they change? What's wrong with having a constructor name being the class name... why did they have to introduce that (ugly) __construct... – Déjà vu Mar 28 '13 at 09:18
  • 1
    @ring0 Probably because the `__construct` syntax is more in line with all the other [magic methods](http://php.net/manual/en/language.oop5.magic.php), which is what the constructor is. Further, this change makes the class name available as a regular class method. – Jonathan Aug 29 '13 at 16:46
  • 7
    @ring0: The most important reason is the general principle of cutting unnecessary dependencies/redundancies. Have you ever found yourself changing class names late at night in the middle of a refactoring effort? And then debugging all through the rest of the night due to forgetting to rename one of the constructors, too (due to your kids interrupting: "Daddy, why don't you sleep? Can we also stay up tonight, canwe?")? With `__construct()`, neither this risk, nor the mere unnecessary extra typing exist any more. And it was even free. – Sz. Apr 02 '14 at 08:37
  • 1
    @lunakid It's a valid point. But these *magic* methods deserve well that attribute... the PHP folks added some '__' to (generally) prevent name collision with an existing method ; is that better? At least in C++ constructors have no return value and thus compile would fail if the class is renamed (in PHP that would not be the case). IMO, the best would have been to create a proper *constructor* keyword instead of the usual *function*, before the actual method name. `constructor myConstruct() {...}`. But that would probably have given the PHP team more work... – Déjà vu Apr 02 '14 at 09:37
7

Constructor function named same as class is a backward compatibility feature for PHP4. It will not work with namespaced classes since PHP 5.3.3

If both __construct and class-named functions are present, then the __construct is used as constructor.

Mchl
  • 61,444
  • 9
  • 118
  • 120
4

The first one is old php4 style "construct". It is basically the same as the __construct.

shaggy
  • 1,708
  • 2
  • 15
  • 17
3

The difference is that calling a constructor by the same name of the class is deprecated.

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
  • It is not deprecated, when using classes without namespace. See approved answer and comments around. – trejder Mar 07 '14 at 13:47
  • @trejder it's deprecated and will be removed https://www.php.net/manual/en/migration70.deprecated.php – hkiame Feb 28 '22 at 12:11
  • @hkiame True, it **is** deprecated **now**. It _wasn't_ deprecated _eight years ago_! :) – trejder Mar 01 '22 at 07:54
2

The difference is that PHP version 5.3.3 and above will treat function foo() as regular method and not constructor. Previous versions will treat it as a constructor.

Michael J.V.
  • 5,499
  • 1
  • 20
  • 16