Why should I usefunction __construct()
instead of function className()
in PHP 5?
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.
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.
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
My guess would be that by the time object-oriented capability was being added to PHP, the designers were looking at Python.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With