致命错误:无法使用[]进行阅读

时间:2015-06-15 07:43:16

标签: php arrays object

我收到此错误

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对象添加到数组中。这很有趣,因为我尝试使用完全相同的语法,在另一个项目中使用相同的方法,使用完全相同的语法。此外,我不明白为什么它说我正在尝试从数组中读取,当我实际添加它时。

有谁知道这里发生了什么?

编辑:我圈出了问题并制作了一个更简单的代码版本,以便您自己查看。

1 个答案:

答案 0 :(得分:1)

So, just to mark this solved, I got rid of the problem simply by rewriting the characters inside the method leggtilUtdanning. Appears to have been some sort of character encoding-problem like you pointed out, but I have absolutely no idea how that happened. Anyway, thanks for all the help.