views:

126

answers:

2

Hey.

I've always assumed that - in the absence of constructor parameters - the parenthesis (curly brackets) follow the class name when creating a class instance, were optional, and that you could include or exclude them at your own personal whim.

That these two statements were equal:

$foo = new bar;
$foo = new bar();

Am I right? Or is there some significance to the brackets that I am unaware of?

Thanks.

P.S.
I know this sounds like a RTFM question, but I've been searching for a while (including the entire PHP OOP section) and I can't seem to find a straight answer. (I fear my Googling skills are getting rusty :-)

+4  A: 

They are equivalent. If you are not coding by any code convention, use which you like better. Personally, I like to leave it out, as it is really just clutter to me.

Gordon
Agreed. If the constructor doesn't take any parameters I leave them out too
AntonioCS
Thanks. I was hoping this was the case. - Personally I like to always include them. Bad habit from my Java/C# days :)
Atli
..also, the end part looks like a smirking singing monkey (); laaaa-la
0scar
+1  A: 

Parenthesis are not required for a class that has no constructor. If you put it, the php is smart enough to know that class you have initiated has no constructor in it. So no problem. You can use either.

Sarfraz
Thanks, but what about classes that *do* have constructors? *(Without parameters though, obviously)* - All my tests indicate that both are equal, but I worry that I might be overlooking something.
Atli
Doesn't have a constructor or the constructor doesn't take any arguments?
AntonioCS
if a certain class has constructor, you need parentheses then you know but otherwise not necessary. You should stick to one convention in this case.
Sarfraz
I might be misunderstanding you, but are you saying classes with a constructors *need* to be created with parenthesis? Because that's not true. Even with a constructor, leaving the parenthesis out seems to invoke the constructor normally. *(Again, this is all assuming there are not parameters needed for the constructor.)*
Atli