Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's difference between __construct and function with same name as class has? [duplicate]

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");
like image 468
genesis Avatar asked Jul 29 '11 12:07

genesis


1 Answers

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

like image 113
Damien Avatar answered Sep 18 '22 19:09

Damien