0

所以,我有一些这样的代码:

$xml = simplexml_load_file( $configuration_path . $this->configuration_file );

// some stuff in between, not important

// next line would be line 80, where the parser whines
$this->ping_tree_1 = ( string ) (( $xml->xpath( '//ping-tree[@order="1"]' ))[0] );

现在,它对我来说似乎足够有效,但我不断收到此错误:

Parse error: syntax error, unexpected '[' in models/site_configuration.php on line 80

如果我注释掉违规行并执行以下操作:

print_r( $xml->xpath( '//ping-tree[@order="1"]' ));
die;

我看到以下内容被返回:

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [order] => 1
                )

            [0] => dd0d7afa2414fc1d3ddc18c87351478f
        )

)

这正是我所期望的。

然而,更有趣是,当我这样做时:

$_xml = $xml->xpath( '//ping-tree[@order="1"]' );
$this->ping_tree_1 = ( string ) $_xml[0];

一切正常

那么......什么给了?有人有线索吗?这是一个 PHP 解析错误,还是只是我很密集?

顺便说一句,这一切都在 Apache/2.2.22 (Unix) 下的 PHP/5.2.17 上。

谢谢!

4

1 回答 1

1

查看有关数组的手册页:http: //php.net/manual/en/language.types.array.php

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable

您正在尝试使用此代码:

$this->ping_tree_1 = ( string ) (( $xml->xpath( '//ping-tree[@order="1"]' ))[0] );

您尝试使用的语法是在 5.4 中引入的,而您正在使用 5.2。你已经看到你可以用一个临时变量来解决这个问题;您的替代方法是更新到更高版本的 PHP。

于 2013-07-15T17:27:02.980 回答