Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiate a class with or without parentheses? [duplicate]

Tags:

php

Possible Duplicate:
PHP class instantiation. To use or not to use the parentheses?

I have not found any official documentation on this. But afaik it doesn't matter whether a class is instantiated with our without the parentheses - as long as there are no parameters involved, right?

$car = new Car; 

or

$car = new Car(); 

But can anyone tell me if there's a difference in performance? Which way is the 'more correct' way? Is there any official documentation for this?

like image 768
Frank Avatar asked Oct 06 '10 13:10

Frank


2 Answers

Any difference in performance is going to be absolutely negligible.

While both ways are fine, I personally would prefer using new Car(); because usually, a method is being called here, and function/method calls in PHP require (). Also, it's more consistent with instantiations that have parameters.

But in the end, it's down to taste. It doesn't matter which way you choose, but when you choose one, stick to it consistently!

like image 120
Pekka Avatar answered Oct 13 '22 04:10

Pekka


the first instantiation has no "official" reference. In the official php doc you alway find the second one. So, i'de prefere this for consistenscy. But it's all your choice

like image 30
ArtoAle Avatar answered Oct 13 '22 06:10

ArtoAle