6

Does PHP allow to call methods off of a new object like this:

new CEntry( new Control() )->actuate();

I can pass a new object in as a parameter, as in new Control(). However, it does not seem to like the actuate() call.

I'm getting error:

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/foo/public_html/develop/foos/source/class.CEntry.php on line 4
Kev
  • 118,037
  • 53
  • 300
  • 385
  • Possible duplicate of [In PHP, can you instantiate an object and call a method on the same line?](http://stackoverflow.com/questions/1402505/in-php-can-you-instantiate-an-object-and-call-a-method-on-the-same-line) – Muhatashim May 14 '16 at 03:13

3 Answers3

5

Not until PHP 5.4, no. In PHP 5.3 and earlier, you'll have to use another variable:

$obj = new CEntry( new Control() );
$obj->actuate();
Ry-
  • 218,210
  • 55
  • 464
  • 476
1

Does PHP allow to call methods off of a new object like this:

new CEntry( new Control() )->actuate();

It does in >= PHP 5.4

alex
  • 479,566
  • 201
  • 878
  • 984
1

For versions less than 5.4, you can actually use this trick by using parenthesis:

(new CEntry( new Control() ))->actuate();
Muhatashim
  • 646
  • 8
  • 22