2014-05-22 53 views
0

我在我的電腦上建立了一個網站,使用最新的PHP版本的XAMPP,一切看起來都很好。現在,我剛剛買了一個域名,虛擬主機等,唯一的問題,我總是在每一個頁面得到的是正是這一點:我有stdclass [括號]的問題,我不知道爲什麼

Parse error: syntax error, unexpected '[', expecting ',' or ';' in /home/cbifina1/public_html/classes/Credit.php on line 61

這是你在該行發現了什麼:

$this->_db->results()[$x]->form_id 

在另一頁,我有同樣的問題,我固定

$this->_db->results()[0]->id 

$this->_db->first()->id 

但用循環中使用的變量,我卡在這裏。

那麼爲什麼我的括號不可讀?我的PHP服務器在PHP版本5.3.28 ..我不認爲這可能是問題..

有什麼建議嗎? :/

EDITED BELOW 5/22/14 ------------------------------------- ------

所以我們可以說我在這裏有這兩個功能

public function results() { 
    return $this->_results; 
} 

public function first() { 
    return $this->results()[0]; 
} 

我怎樣才能下方5.4去掉,如果我使用[0] PHP的?

+0

這種用法(_db->結果()[$ X])是PHP 5.4的功能,因此它是一個版本的問題 – engvrdr

回答

2

實際上,您需要5.4才能使用該構造。 The docs說;

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.

編輯:關於如何更改5.3代碼的後續處理需要使用臨時變量和索引來代替。這意味着翻譯;

public function first() { 
    return $this->results()[0]; 
} 

...到...

public function first() { 
    $tmp = $this->results(); 
    return $tmp[0]; 
} 
+0

我編輯題! –

+0

@matt_bois更新回答您的後續問題。 –

相關問題