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 に答える