2017-06-13 25 views
-3

我發現了一個PHP庫在其上線爲:

$this->get_soap_provider_options()['location']; 

但是,這是生產的錯誤:

Parse error: syntax error, unexpected '[' in ...path to file.. at line... 

ie:我不明白爲什麼['location']是在功能修正get_soap_provider_options()之後編寫的。它應該是get_soap_provider_options('location')get_soap_provider_options(array('location'))或類似的東西。

我認爲這行代碼適用於php 5.4或更高版本。我該怎麼寫這個老版本的PHP?

+0

'get_soap_provider_options()'返回一個數組並嘗試訪問的值(在陣列內)通過鍵'location'。 –

+0

http://php.net/manual/en/language.types.array.php#language.types.array.syntax.accessing,example 7. –

+0

Tom,如何編寫$ this-> get_soap_provider_options()['位置']線的舊版本(5.4之前)?因爲在關閉參數大括號之後,不允許在舊版本的php中寫入'['location']''。它應該是這樣的:'$ this-> get_soap_provider_options('location')'或'$ this-> get_soap_provider_options(array('location'))'.......怎麼寫? –

回答

0

你說的是關於PHP> = 5.4是正確的。該語法用於訪問函數返回的數組。這被稱爲array dereferencing(來自函數或方法)。

要訪問數組中的值PHP < 5.4您必須將函數或方法的結果存儲在變量中。此變量將作爲一個「臨時」變量(參見文檔上實施例7):

$tmp = $this->get_soap_provider_options(); 
// now you can use > $tmp['location']; < 
//     ^^^^^^^^^^^^^^^^^