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

Objects in PHP Forum

View Course » View Exercise

423 points
52c54306548c35fbc5007fe3_167165382
Submitted by
Damon Harris
about 2 years ago

Is this code correct?

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


Reconstructing the Person Class




<?php
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
        ?>
      </p>
    </body>
</html>

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

permalink

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. .

426 points
9a74e1277d8e32358137a88e715f067c?s=140&d=retro
Submitted by
Bret Wagner
about 2 years ago

1 Comment

52c54306548c35fbc5007fe3_167165382 Damon Harris about 2 years ago

Perfect! Thanks! :D