This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Objects in PHP Forum

View Course » View Exercise

1046 points
Picture
Submitted by
cseto
about 4 years ago

[resolved] Undefined variable: Alive (line 19) Fatal error: Cannot access empty property

Code passes but I get the Fatal error message in the browser.

<?php 
        class Person{
        public $isAlive=true;
        public $firstname;
        public $lastname;
        public $age;
        };
        $teacher = new Person();
        $student = new Person();
        echo $teacher->$isAlive;
        ?>

1 vote

permalink

When you are accessing a property of an object, the correct syntax is:

$obj->propname;

In this case you are accessing the $isAlive property of the $teacher object so match up the syntax and you should be fine.

3698 points
55df2afa95e3787b9200005e_359368177
Submitted by
Judy
about 4 years ago

4 Comments

Picture cseto about 4 years ago

Ah thanks. Caught it.

55df2afa95e3787b9200005e_359368177 Judy about 4 years ago

You're welcome.

Picture Rory Clancy about 4 years ago

I've put in petty much the same code and I'm getting the error message "Undefined variable: isAlive (line 19) Fatal error: Cannot access empty property on line 19"
what am i doing wrong

55df2afa95e3787b9200005e_359368177 Judy about 4 years ago

Hi Rory, even though there are several ways to solve coding tasks the syntax is very specific, you'll need to post your code if you want someone to troubleshoot with you.


0 votes

permalink

Please remove ; from };

365 points
543fc82f631fe988640020d7_382532424
Submitted by
Tobias Páez
about 3 years ago