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

Objects in PHP Forum

View Course » View Exercise

423 points
Submitted by
Damon Harris
over 2 years ago

Is this code correct?

Hello, I tried this code:
<!DOCTYPE html>

Reconstructing the Person Class

class Person {
public $isAlive = true;
public $firstname;
public $lastname;
public $age;

            public function __construct($firstname, $lastname, $age) {
                $this->isAlive = $isAlive;
                $this->firstname = $firstname;
                $this->lastname = $lastname;

        $teacher = new Person("boring", "12345", 12345);
        $student = new Person("Damon", "Harris", 14);

        echo $teacher->isAlive

I clicked save and submit and it worked but the preview panel shows:

Undefined variable: isAlive (line 17)Undefined variable: isAlive (line 17)

Is the code correct?

P.S The code markdown is broken

0 votes


you don't need the line "$this->isAlive = $isAlive;"

$isAlive already equals true, this has already been declared so your __construct function doesn't need it. .

436 points
Submitted by
Bret Wagner
over 2 years ago

1 Comment

52c54306548c35fbc5007fe3_167165382 Damon Harris over 2 years ago

Perfect! Thanks! :D