DEV Community

Lalit Kumar
Lalit Kumar

Posted on

Syntax error, unexpected '->' (t_object_operator).

It is not possible to call a method on an object created with new before PHP 5.4. When the user tries to call a method on an object created with the new then the error occurs;


title: "Syntax error, unexpected '->' (t_object_operator)."
tags: php

canonical_url: https://kodlogs.com/blog/359/syntax-error-unexpected-t_object_operator

“PHP Parse error: syntax error, unexpected ‘->’ (T_OBJECT_OPERATOR) ” in get_info.php on line 16.

Solution:

To solve this error the user needs to switch PHP version through your hosting control panel if possible. Or contact your hosting provider to provide the support to switch PHP version.

PHP 5.4:

In PHP 5.4 or later the following can be used;

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

Previous Versions:

In previous versions, you have to call the methods on the variables like;

$purchaseFactory = new PurchaseOrderFactory; // new keyword will initialize new object of the class PurchaseOrderFactory

$purchaseOrder = $purchaseFactory->instance();
You can also this error by changing your syntax like;

$purchaseOrder = PurchaseOrderFactory::instance();

$arrOrderDetails = $purchaseOrder->load($customerName);
Simply use the instance() in place of new. This will create the instance of the class.

Error in array index:

Use => instead of -> to assign the value to array index. You also need to replace Jason_encode($response) with json_encode($response)

Example:

array_push($response, array(“id”=> $row[‘id’], ‘title’=>$row[‘name’]));

Top comments (0)