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

Objects in PHP Forum

View Course » View Exercise

418 points
Picture
Submitted by
Pjotr Leshkin
about 2 years ago

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

Written code

<!DOCTYPE html>
<html>
    <head>
      <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
    </head>
    <body>
      <p>
        <!-- Your code here -->
            <?php
       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;
       ?>

       ?>
       ?
      </p>
    </body>
</html>

and browser prints out

Parse error: syntax error, unexpected TOBJECTOPERATOR on line 17

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


1 vote

permalink

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
Cbadd5f534ea698d0f4a98fc15b20a64?s=140&d=retro
Submitted by
xMichi1996x
about 2 years ago


1 vote

permalink

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
Ccbb2c3d0fbb5a30bbcb52691b529158?s=140&d=retro
Submitted by
justin rescorl
about 2 years ago