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.
See Question&Answers more detail:os