2

Usually I create a new object invocating his constructor the usual way:

$instance= new Class();

Lately, I'm reading a lot of code with an alternative syntax (without the parentesys):

$instance= new Class;

There are substantial differences between the two methods or are they equivalent?

I haven't found references on this topic 'till now.

2
  • 1
    As far as I know, unless you need to pass parameters to your constructor, both expressions are identical. Correct me if I am wrong. Commented Jan 28, 2010 at 0:45
  • As far I know, I agree with you. I would like to be sure this is an intended feature, though. Commented Jan 28, 2010 at 1:27

2 Answers 2

5

There is no difference.

You're right about this being hard to prove. The Basics introduces the new keyword but all examples have parentheses and no mention is made of their optionality.

Edit: best I can find are examples from the official documentation such as:

// This will call B() as a constructor
$b = new B;
Sign up to request clarification or add additional context in comments.

1 Comment

Just what I was searching. Thanks
0

I am not familiar with PHP, but the latter probably invokes the default constructor, while the former invokes the constructor with no parameters.

2 Comments

Unfortunately, PHP doesn't support multiple constructors. Nice try though. :)
Re: No multiple constructors. True, however you can omit them and if default values are provided in the signature they will be substituted in when the call is made.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.