(在PHP中)這是我的問題,我想在我的類中初始化一個數組,然後使用構造函數填充它,然後我可以在其他數組中使用該數組的變量函數..當我在構造函數中迴應我的數組時,它完美地工作,但是當我嘗試在另一個函數中回顯它時,它給了我非常不同的東西。試圖從一個類中的多個函數調用一個數組
class myProblem
{
public $phaseArray;
function myProblem()
{
$count1 = 0;
$metaFile = fopen('MyFile.txt', 'r') or exit("Unable to open file!");
while(!feof($metaFile))
{
$this->phaseArray[0][$count1] = 0;
$this->phaseArray[1][$count1] = fgets($metaFile);
echo $this->phaseArray[1][$count1], $count1, '</br>'; //this part displays well
$count1++;
}
close($metaFile);
}
function displayError()
{
foreach($this->phaseArray as $key => $value)
{
echo $key, $value, '</br>'; //this part does not show up correctly
}
echo $this->phaseArray[0][2]; //this part does not show up correctly
echo $this->phaseArray[1][1]; //this part does not show up correctly
}
}
對於縮進,我不能得到它的工作。 正確的打印輸出是(來自構造函數); 0Apple 1Orange 3Pear 4Strawberry
但第二功能顯示; 0Array 1Array
0Array 0Array
什麼我做錯了什麼想法?感謝您的時間!
注意:您應該使用__construct()作爲構造函數類的名稱。類名稱風格與PHP5一樣被棄用。 –
你可以發佈你如何調用函數。 –
謝謝Alex,我不知道。我會解決這個問題。 –