Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PHP 5 use __contruct() instead of className() as constructor?

Tags:

oop

php

Why should I usefunction __construct() instead of function className() in PHP 5?

like image 241
Ali Avatar asked Oct 06 '09 17:10

Ali


4 Answers

The __ magic methods/functions seem to be a consistent theme in PHP (for once!). One advantage of using __construct() over ClassName() as a constructor is if you change the name of the class, you don't need to update the constructor.

like image 116
Asaph Avatar answered Sep 28 '22 10:09

Asaph


Because php5 wanted to be more like python.

I kid, I kid...

Having a standard method for standard actions, like construction, is a reasonable solution. It's the same reason that in C# classes, when you extend a class, you use base for calling base class constructors instead of a named object: it simplifies code and makes maintenance easier.

like image 44
Robert P Avatar answered Sep 28 '22 10:09

Robert P


Because it has been unified with the __destruct() method and other special methods beginning with two underscores for example __get, __sleep, __serialize http://us2.php.net/manual/en/language.oop5.magic.php

like image 33
friederbluemle Avatar answered Sep 28 '22 10:09

friederbluemle


My guess would be that by the time object-oriented capability was being added to PHP, the designers were looking at Python.

like image 36
Tim Sylvester Avatar answered Sep 28 '22 10:09

Tim Sylvester