1

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

I'm learning PHP from an O'reilly book "Learning PHP, MySQL and Javascript". I'm half way through learning about objects etc.

I've noticed that sometimes the writer calls objects in two different ways.

  • $example = new Object
  • $example = new Object()

They both seem to work fine for me. Is there a preferred way or best practice? Is there a reason for this?

Community
  • 1
  • 1
LondonGuy
  • 10,778
  • 11
  • 79
  • 151

1 Answers1

2

Personally I prefer to include the parenthesis. helps to remind you the constructor is a method that you can override.

But this could just be because I learned OOP in C++.

Whatever you do, just be consistent.

Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
  • It's funny that you say that because C++ doesn't require parentheses; `new string;` is fine. Some people will say that omitting parentheses is the best practice for C++, in fact. (I think the reasoning is that it avoids ambiguity in the rare case where a function has the same identifier. Doesn't seem like a big deal to me.) – Daniel Lubarov May 16 '11 at 23:38
  • @Daniel - Interesting. Learn somthing new everyday. – Byron Whitlock May 16 '11 at 23:42