0

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.

SudokuNinja
  • 419
  • 1
  • 6
  • 16
  • 3
    @LeZohan68 Add an array value to the property `$utdanning` maybe? – Daan Jun 15 '15 at 07:46
  • 2
    @LeZohan68 It's not the same variable name. One is named `$this->utdanning`, the other `$utdanning`. – deceze Jun 15 '15 at 07:48
  • I'm basically trying to add the object (they're the same variable-name, but different ones still) used as the parameter on the function and add it to the private array $utdanning. I'm sort of learning as I go here, working on a project, so forgive me if there's something completely irrational going on here. I tried changing the name of the parameter without any solving of the problem. – SudokuNinja Jun 15 '15 at 07:49
  • 3
    @user Please provide us with a **complete** reproducible test case. The code you're showing doesn't actually do anything, because it's never actually executed. There's likely more going on in your actual code which causes the problem. This here should work fine as is. – deceze Jun 15 '15 at 07:49
  • 1
    Works as expected for me. – joconja Jun 15 '15 at 07:50
  • 4
    @LeZohan68 please resist from answering questions on subjects you are apparently not familiar with – Marcin Orlowski Jun 15 '15 at 07:57
  • 1
    @MarcinOrlowski that's why i used comments and not answer. + I'm just trying to help. If you're not ok with that ... – Guillaume Munsch Jun 15 '15 at 07:58
  • Thanks for your help everyone. @deceze I added *most* of the code. I'm basically making a HTML-form with object-orientation. – SudokuNinja Jun 15 '15 at 08:01
  • 1
    This particular code is a syntax error, due to an `include` inside `class{}`. – deceze Jun 15 '15 at 08:09
  • @deceze I removed this include now. Was basically just trying to figure out whether the error came from that I needed to include the Utdanning-class inside the Person-class to reference it. – SudokuNinja Jun 15 '15 at 08:13
  • The error remains however. – SudokuNinja Jun 15 '15 at 08:14
  • 1
    Well, what this says is that this is still not an actual reproducible case to demonstrate the problem, since this code can never run as is. Please boil it down to **the smallest amount of code that demonstrates the problem**. Adding more code doesn't help anyone. Reduce it until you've circled in on the problem. – deceze Jun 15 '15 at 08:15
  • 1
    @deceze Sure. I'll make an edit later on when I've been able to make a smaller version of the problem. – SudokuNinja Jun 15 '15 at 08:32
  • @deceze So I edited the question and there is a lot less code. Should be a lot simpler to debug now. – SudokuNinja Jun 15 '15 at 08:52
  • 1
    What is you PHP version? Just tried your example on mine and all goes fine (PHP 5.5.25). – Alexey Eremikhin Jun 15 '15 at 09:00
  • @Alexey Eremikhin I'm using PHP version 5.5.3 – SudokuNinja Jun 15 '15 at 09:02
  • 2
    Invalid characters in the source code (UTF-8)? I copied and pasted the supplied code and it works fine here (PHP 5.3.29). – Ryan Vincent Jun 15 '15 at 09:26
  • @Ryan Vincent Hmm, strange.. What do you mean exactly? I don't have any special characters in the OP code-example as far as I'm aware.. I've tried running it both on my personal web server and through Apache. Still getting the error. – SudokuNinja Jun 15 '15 at 09:33
  • 1
    Well, works fine as is: http://3v4l.org/G7flR. If this exact code is giving you errors, either you're not actually running *this* code (for example because there are some invisible characters in the source), or your server is mightily screwed up. – deceze Jun 15 '15 at 09:34
  • 1
    Look at the source file with a hex editor and check that the characters on the line where the error is being report actually has correct ASCII character codes. Also: [PHP source code in UTF-8 files; how to interpret properly?](http://stackoverflow.com/questions/17872046/php-source-code-in-utf-8-files-how-to-interpret-properly) – Ryan Vincent Jun 15 '15 at 09:42
  • @Ryan Vincent Well, that's weird, I copy-pasted my own code from the OP into my editor and ran the code, and now it's working for some reason. I have a separate file with code that appears to be the exact same, but is not working. I'm guessing this must be character-encoding issues like you mentioned? – SudokuNinja Jun 15 '15 at 09:47
  • @deceze It appears to be the former. – SudokuNinja Jun 15 '15 at 09:50
  • 1
    It happens to us with our 'editng software' sometimes. ;-/ Thanks for adding your answer. How do you recognize it if it happens again? I have no easy answer. Alas, it can waste a lot of your time. Copy and paste the text into some really simple text editor and run it again may be the easiest way of checking. ;-/ – Ryan Vincent Jun 15 '15 at 16:37
  • @Ryan Vincent No problem. I'll mark it solved once I get past the two days-limit (apparently this is a thing when you make your own answers...) Thanks again for your input which made me work this out. It's definitely a very time-consuming thing when you happen upon bugs like this, yeah. That text-editor method might be the best way. At least I'm now aware these types of bugs can occur as well. – SudokuNinja Jun 15 '15 at 20:58

1 Answers1

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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
SudokuNinja
  • 419
  • 1
  • 6
  • 16
  • You should accept your answer by clicking on the green tick. This will record that your problem has been solved by Stack Overflow and will give you points as described in the [tour]. – Brian Tompsett - 汤莱恩 Jun 26 '15 at 17:51