4

Does anyone know if the following expression will be possible in the next version(s) of PHP?

(new A())->a();    // Causes a syntax error

I find very annoying that currently one must write 2 lines instead of 1:

$c = new A();
$c->a();
Ivan Krechetov
  • 18,802
  • 8
  • 49
  • 60
  • possible duplicate of [How to chain method on a newly created object?](http://stackoverflow.com/questions/2188629/how-to-chain-method-on-a-newly-created-object) – artfulrobot Jan 28 '15 at 09:52

3 Answers3

7

The first version does not cause a parse error, it is perfectly valid. The second it is, indeed not possible, but you can easily overcome such a problem with some coding standards.

If every team member creates, for each defined class, a function with the same name as the class, and a signature similar to the signature of the class constructor, then you won't have the second problem. Example:

class Foo
{
    public function __construct($param)
    {}

    public function bar()
    {}
}

/**
 * @return Foo
 */
function Foo($param)
{
    return new Foo($param);
}

Foo()->bar();

Of course, you'll still have problems with library code.

Ionuț G. Stan
  • 176,118
  • 18
  • 189
  • 202
  • Right, the first one is non-issue. I must have confused it with something else. I'll edit the question. Thanks – Ivan Krechetov Jan 27 '09 at 08:39
  • I think you confused it with functionReturnsArray()[3], which is indeed not possible. There have been proposals, but they didn't get accepted. PHP internals don't like that shortcut. – Ionuț G. Stan Jan 27 '09 at 08:48
  • Iount: do you have any links to the discussing regarding this? This is a feature I really miss :( – Jan Hančič Jan 27 '09 at 11:08
  • http://markmail.org/message/kk2hwn3gdjcw2yfh#query:related%3Akk2hwn3gdjcw2yfh+page:1+mid:5bv6mtd7zorni7gc+state:results http://wiki.php.net/rfc/functionarraydereferencing – Ionuț G. Stan Jan 27 '09 at 11:23
  • Can anyone tell me if this is a common idiom in PHP? Do people often do this,that is define a global fn that returns the object? – Aaron May 14 '10 at 00:45
  • No, it's not a common idiom. I've seen it used by me and just one other person. – Ionuț G. Stan May 14 '10 at 21:33
  • "The first version does not cause a parse error, it is perfectly valid." Wish Stack Overflow had visible revisions so I could see what you're talking about here before it was edited. EDIT: Found them! Just need to click on the date. <3 – mattalxndr Sep 10 '10 at 06:00
6

A new-expression can be used as a function argument. A function call can be used as the left-hand side of the member access operator. Therefore, you just need to define a single function:

function with($object) { return $object; }

with(new A()) -> a();

No additional efforts required on a per-class basis.

  • I like this, although it ends up being a bit weird if you also need a reference to the object for later use. Unless the method(s) you call return a reference to the object (e.g. are specifically chainable), then you need to do `with($my_object = new A())->a();`. But it's a novel way around a limitation, thanks! – artfulrobot Jan 28 '15 at 09:48
1

This is possible as of PHP 5.4+:

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

colan
  • 2,818
  • 2
  • 20
  • 17