2

It's really annoying that you can't do any of these things (and it doesn't make any sense that you can't):

new A('foo')->bar();

(new A('foo'))->bar();

The only thing I could think of is to have a static function to return a new object

public static function get($a) {
  return new self($a);
}
// ...
A::get('foo')->bar();

But that's just ugly. The reason why I need this is because in the context of the object definition I mostly pass the new object as parameter or as part of an array:

new B(array(
   new A('foo')->bar()
   new A('smt')->bar()->more()
));

bar() and more() of course return a reference to the object.

2
  • 2
    I agree and sympathize, but what is your question? I think this can't be circumvented... Commented Dec 18, 2010 at 17:24
  • I was hoping there is a pattern how to do this without cluttering your code with a static function that basically does nothing. Commented Dec 18, 2010 at 17:29

3 Answers 3

2

This isn't an answer to "how to do this without cluttering your code…" but hopefully shows some light at the end of the tunnel.

There has been a recent RFC on this particular topic (see also the related developer discussion) and while there are a few points to iron out, the response was very favourable.

In the mean time, you will have to stick with your factories whether you like them or not.

Sign up to request clarification or add additional context in comments.

Comments

2

In PHP 5.4 this has now been made possible and thus requires no workarounds: http://www.php.net/manual/en/migration54.new-features.php

PHP 5.4.0 offers a wide range of new features:

Class member access on instantiation has been added, e.g. (new Foo)->bar().

2 Comments

Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
I wrote my answer believing that the satisfying answer would be just to say that "now it is possible, from version x onwards", and the link was just for the reference of that. Anyway, kmas has already amended the section to the answer, but as you can see, that is only an exact copy of the question, because the question itself now is also the answer.
1

No, as far as I know, using a function like you show is the only way around this.

I'm no Guru in PHP's internal workings, but the architectonical reason for this is probably that new is a language construct, and can accept different kinds of expressions. Therefore,

 new A('foo')->bar()

would be ambigous: Is the new object intended to be of the class A, or does the class name come from bar()'s return value?

1 Comment

You are right. In fact I wouldn't actually expect this to work. That's why I added the example with correct brackets (new A('foo'))->bar()

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.