PHP 解析错误:语法错误,意外的 T_OBJECT_OPERATOR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13388541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:18:44  来源:igfitidea点击:

PHP Parse error: syntax error, unexpected T_OBJECT_OPERATOR

php

提问by user1825110

I got this error when debugging my code:

调试代码时出现此错误:

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

PHP 解析错误:语法错误,第 72 行 order.php 中的意外 T_OBJECT_OPERATOR

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

这是代码片段(从第 72 行开始):

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

回答by SirDarius

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

不幸的是,无法new在 PHP 5.4 之前创建的对象上调用方法。

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

在 PHP 5.4 及更高版本中,可以使用以下内容:

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

回答by Samuel Cook

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

其中大概instance()创建了类的一个实例。你可以这样做而不是说new

回答by Vyktor

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

您不能使用(这是无效的 php 语法):

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