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

Objects in PHP Forum

View Course » View Exercise

418 points
Submitted by
Pjotr Leshkin
about 2 years ago

Codecademy accepts exercise, but the build-in browser shows a mistake

Written code

<!DOCTYPE html>
      <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
        <!-- Your code here -->
       class Person {
           public $isAlive = true;
           public $firstname;
           public $lastname;
           public $age;
     public function  __construct($firstname, $lastname,$age) {
        this->$firstname = $firstname ;
        this->$lastname  = $lastname ; 
        this->$age = $age;



            $teacher = new Person('boring','12345','12345');
       $student = new Person('smart', '54321','54321');
       echo $teacher->isAlive;
       echo $student->age;


and browser prints out

Parse error: syntax error, unexpected TOBJECTOPERATOR on line 17

17th line is this->$firstname = $firstname ;

1 vote


yes you have to write: this->firstname = $firstname ;
you have to correct the same at line 18 and 19

I hope this will help

498 points
Submitted by
about 2 years ago

1 vote


you have yer $ in the wrong place. you want something like

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

400 points
Submitted by
justin rescorl
about 2 years ago