2012-01-09 18 views
0

我已經構建了一個包含各種錯誤的函數。我想將該函數移至config.php文件。訪問php類中的配置文件數據

如何繼續使用該功能現在它在config.php裏面?

功能:

private function error($errnum=1000) { 
    $data = array(
     '1000' => 'Required parameter is missing', 
     '1100' => 'Parameter not recognized', 
     '2000' => 'Currency type not recognized', 
     '2100' => 'Currency amount must be to 2 decimal places', 
     '2200' => 'Currencies cannot be the same',  
     '3000' => 'Service currently unavailable', 
     '3100' => 'Error in service' 
    ); 
    $this->result($data[$errnum], $errnum); 
} 

我嘗試使用:

require_once( 「配置/ config.php中」);

在類文件,但它仍然顯示錯誤

解析錯誤:語法錯誤,意想不到的T_PRIVATE

+0

我還想指出,對於一個*函數來說'error'是一個稍微過於泛化/模棱兩可的名字。 – mario 2012-01-09 13:38:57

回答

2

如果您在Config.php文件中使用它,您必須刪除私人部分。

然後,您必須包含用於顯示結果的類實例。或者,您必須更換$this->result($data[$errnum], $errnum);,才能找到不在班級中的東西。

因此,例如,這樣的事情:

function error($errnum=1000) { 
    $data = array(
     '1000' => 'Required parameter is missing', 
     '1100' => 'Parameter not recognized', 
     '2000' => 'Currency type not recognized', 
     '2100' => 'Currency amount must be to 2 decimal places', 
     '2200' => 'Currencies cannot be the same',  
     '3000' => 'Service currently unavailable', 
     '3100' => 'Error in service' 
    ); 
    echo "Error: ".$data[$errnum]."(".$errnum.")"; 
} 

error(2000); 

希望它能幫助。

1

公共,保護和私有隻需要內部類。你的功能不是一種方法,而是一個獨立的功能,因此私人在那裏是無效的。將其移入課程或移除關鍵字。

+0

謝謝。我刪除了私人。儘管嘗試訪問該函數時仍然出現錯誤。返回$ this-> error(1000);是我正在使用的。那有什麼問題? – tctc91 2012-01-09 13:35:55

+0

'$ this'也只在課堂上有意義。 – cmbuckley 2012-01-09 13:37:33