2015-06-15 247 views
0

我得到這個錯誤致命錯誤:無法使用[]閱讀

Fatal error: Cannot use [] for reading in... on line 26

檢查所有已發在這裏對這個錯誤的線程,我仍然無法弄清楚。看着我的代碼,沒有什麼我做錯了。

<?php 
class Person 
{ 
    //Variables for personal information// 
    private $navn; 
    private $adresse; 
    private $postnummer; 
    private $poststed; 
    private $telefonnummer; 
    private $fodselsdato; 
    public function __construct($navn, $adresse, $postnummer, $poststed, $telefonnummer, $fodselsdato) 
    { 
     $this->navn = $navn; 
     $this->adresse = $adresse; 
     $this->postnummer = $postnummer; 
     $this->poststed = $poststed; 
     $this->telefonnummer = $telefonnummer; 
     $this->fodselsdato = $fodselsdato; 
    } 
    //Creates an array to store education for a person// 
    private $utdanning = array(); 

    //Function to add education to the array// 
    public function leggTilUtdanning(Utdanning $utdanning) 
    { 
     $this->utdanning[] = $utdanning; 
    } 
} 
//Class for education 
class Utdanning 
{ 
    private $institusjon; 
    private $studieretning; 
    private $grad; 
    private $startet; 
    private $ferdig; 

    public function __construct($institusjon, $studieretning, $grad, $startet, $ferdig) 
    { 
     $this->institusjon = $institusjon; 
     $this->studieretning = $studieretning; 
     $this->grad = $grad; 
     $this->startet = $startet; 
     $this->ferdig = $ferdig; 
    } 
} 
$person1 = new Person('Dave Lewis', 'Downing Street 14', 0442, 'Northville', 98765432, '17.05.1975'); 
$utdanning = new Utdanning('Harvard', 'Economics', 'Bachelor', 2013, 2016); 
$person1->leggTilUtdanning($utdanning); 
?> 

錯誤來自函數裏面的行,我試圖添加一個Utdanning對象到數組。這很有趣,因爲我嘗試了在另一個項目上使用完全相同的語法來完成它的相同方法,並且它完美地工作。此外,我不明白爲什麼它說我試圖從數組中讀取數據,當我實際添加到數組中時。

有沒有人有一個想法這裏發生了什麼?

編輯︰我盤問這個問題,並提出了一個更簡單的代碼版本,所以你可以看到自己。

+3

@ LeZohan68向屬性'$ utdanning'添加一個數組值?也許吧? – Daan

+2

@ LeZohan68這不是同一個變量名。一個名爲'$ this-> utdanning',另一個'$ utdanning'。 – deceze

+0

我基本上試圖添加對象(它們是相同的變量名稱,但仍然不同)作爲該函數的參數,並將其添加到專用數組$ utdanning。當我到這裏工作一個項目時,我有點學習,所以如果在這裏發生一些完全不合理的事情,請原諒我。我試着改變參數的名字而不解決問題。 – SudokuNinja

回答

1

所以,只是爲了標記這個問題,我簡單地通過重寫方法leggtilUtdanning中的字符來解決問題。看起來像是你指出的某種字符編碼問題,但我完全不知道這是怎麼回事。無論如何,感謝所有的幫助。

+0

你應該點擊綠色的勾號接受你的答案。這會記錄你的問題已經被Stack Overflow解決了,並且會按照[遊覽]中的描述給你提供點數。 –

相關問題