PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR

Unfortunately, it is not possible to call a method on an object just created with new before PHP 5.4.

In PHP 5.4 and later, the following can be used:

$purchaseOrder = (new PurchaseOrderFactory)->instance();

Note the mandatory pair of parenthesis.

In previous versions, you have to call the method on a variable:

$purchaseFactory = new PurchaseOrderFactory;
$purchaseOrder = $purchaseFactory->instance();

Leave a Comment