PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR

前端 未结 3 1506
长情又很酷
长情又很酷 2020-11-28 10:41

I got this error when debugging my code:

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

He

相关标签:
3条回答
  • 2020-11-28 10:43

    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();
    
    0 讨论(0)
  • 2020-11-28 10:58

    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

    0 讨论(0)
  • 2020-11-28 11:00

    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();
    
    0 讨论(0)
提交回复
热议问题