PHP Parse error: syntax error, unexpected T

I got this error when debugging my code:

PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR in order.php on line 72

Here is a snippet of the code (starting on line 72):

$purchaseOrder = new PurchaseOrderFactory->instance();
$arrOrderDetails = $purchaseOrder->load($customerName);

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();

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

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

Note: The later is probably even more useful/wise even after you've upgraded to PHP 5.4 because those two lines can be better separated and there is less code containing a hard-encoded classname, here the name of the factory class PurchaseOrderFactory . This will make you more fluent maintaining the code over time.


change to as your syntax was invalid:

$purchaseOrder = PurchaseOrderFactory::instance();
$arrOrderDetails = $purchaseOrder->load($customerName);

where presumably instance() creates an instance of the class. You can do this rather than saying new


You can't use (it's invalid php syntax):

new PurchaseOrderFactory->instance();

You probably meant one of those:

// Initialize new object of class PurchaseOrderFactory
new PurchaseOrderFactory(); 

// Clone instance of already existing PurchaseOrderFactory
clone  PurchaseOrderFactory::instance();

// Simply use one instance
PurchaseOrderFactory::instance();

// Initialize new object and that use one of its methods
$tmp = new PurchaseOrderFactory();
$tmp->instance();
链接地址: http://www.djcxy.com/p/69236.html

上一篇: PHP解析错误:语法错误,PHP 5.5.9中意外的文件结束

下一篇: PHP解析错误:语法错误,意外的T