Reputation: 439
I'm getting this error
Fatal error: Cannot use [] for reading in... on line 26
Checking all the threads that have been made here on this error, I still cannot figure it out. Looking at my code there's nothing I'm doing wrong.
<?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);
?>
The error comes from the line inside the function where I'm trying to add an Utdanning-object to the array. It's funny, cause I tried this very same method of doing it on another project, using the exact same syntax, and it worked perfectly. Furthermore, I don't understand why it says I'm trying to read from the array, when I'm actually adding to it.
Does anyone have an idea what's going on here?
EDIT: I circled the problem and made a more simple version of the code so you can see for yourself.
Upvotes: 0
Views: 1487
Reputation: 439
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.
Upvotes: 1