语法错误,意外的T_OBJECT_OPERATOR

时间:2022-07-17 22:31:33

I'm getting the following error when trying to add some data from myXml.xml to a string: Parse error: syntax error, unexpected T_OBJECT_OPERATOR.

当尝试从myXml添加一些数据时,我将得到以下错误。xml到字符串:解析错误:语法错误,意外的T_OBJECT_OPERATOR。

    $xmlstr = file_get_contents('myXml.xml');
    $xml = new SimpleXMLElement($xmlstr); 

    foreach($xml->order as $order){
            $replace = array();
            $firstName = (string) $order->billing-address->first-name;
            $lastName = (string) $order->billing-address->last-name;
    }

I can't provide my XML directly as it contains sensitive data.

我不能直接提供XML,因为它包含敏感数据。

Thanks, Sam

谢谢你,山姆

1 个解决方案

#1


12  

The - sign means subtraction. To use it in property names, you must use this syntax:

负号表示减法。要在属性名中使用它,必须使用以下语法:

$firstName = (string) $order->{"billing-address"}->{"first-name"};
$lastName = (string) $order->{"billing-address"}->{"last-name"};

In general, it's probably better to use firstName, billingAddress, etc. as property names to avoid this. See CamelCase. In this case, however, you may have no control over the the XML input.

通常,最好使用firstName、billingAddress等作为属性名,以避免这种情况。看到CamelCase。然而,在这种情况下,您可能无法控制XML输入。

#1


12  

The - sign means subtraction. To use it in property names, you must use this syntax:

负号表示减法。要在属性名中使用它,必须使用以下语法:

$firstName = (string) $order->{"billing-address"}->{"first-name"};
$lastName = (string) $order->{"billing-address"}->{"last-name"};

In general, it's probably better to use firstName, billingAddress, etc. as property names to avoid this. See CamelCase. In this case, however, you may have no control over the the XML input.

通常,最好使用firstName、billingAddress等作为属性名,以避免这种情况。看到CamelCase。然而,在这种情况下,您可能无法控制XML输入。