tags:

views:

314

answers:

6

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

+2  A: 

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.

Robert P
+21  A: 

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.

Asaph
Does `__autoload()` work regardless if you use `_construct()` or `className()`?
Click Upvote
I wish C++ and Java used the ctor()/dtor() method signature. :-/
Chris Kaminski
@Click Upvote Yes. __autoload() shouldn't care which style of constructor was used.
Asaph
@darth, thankgod they don't use those ugly methods!
Click Upvote
@Click Upvote: `__autoload()` doesn't instantiate a class, so frankly it doesn't care whether you use `__construct()`, `CLASSNAME()`, or ignore the constructor completely. All `__autoload()` does is if a class is NOT defined, it reads the file that defines it. It's up to you to use the `new` keyword or whatever.
dcousineau
Thanks dcousinau , someone told me that `__construct()` had something to do with `__autoload()`
Click Upvote
Strictly speaking, "all __autoload()" does is get called whenever a class or interface that isn't defined is first referenced. It's up to the implementation to attempt to ensure that the definition is sorted out; it could just as well do nothing, although that would break the contract.
Rob
+4  A: 

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

friederbluemle
+2  A: 

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

Tim Sylvester
@Click Upvote Interesting that you selected this as the correct answer. It doesn't even technically answer your question "Why should I usefunction __construct() instead of function className() in PHP 5?"
Asaph
Indeed, I was really just being facetious. Asaph's answer is certainly much more helpful.
Tim Sylvester
A: 

By doing so always, you can invoke the constructor from the super (base-)class without having to know its name. This is very useful for class tree maintenance, because you don't want to have to update all your classes just because you re-arrange your class trees

and...just guessing.. if all classes name their constructors identically __construct(), in theory a constructor could be inherited from a superclass without any required definition. This makes sense in class trees where there are intermediate abstract classes, and in constructs like in objective C where default constructor behaviour is derived entirely from class metadata and therefore (in priciple!) would need no coding at all.

richard bredero
A: 

Old question, but I'll bite since no one has actually answered the actual question yet.

function className() is a PHP4-style constructor.

function __construct() is a PHP5-style constructor.

You should use the latter because the former is deprecated and may be removed from the language.

Also, the former may or may not ignore various PHP5 OO concepts, such as the public/private visibility operators. Not that you'd want to make the constructor private if you weren't using the Singleton or Factory patterns.

R. Bemrose