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

Objects in PHP Forum

View Course » View Exercise

400 points
Submitted by
Mahmoud Khamis
over 3 years ago

why it doesn't work ?

it supposed to echo $student $age which it doesn't , i just get 1
and i get this message "I don't see your constructor. Did you also mind the 'function' keyword? Please check also, whether you included both underscores ('__')."
here is my code

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("Mahmoud", "Khamis", 24);
    echo $teacher->isAlive;
    echo $student->age;

0 votes


Hey friend! Remove the $ in your constructor so;
public function __construct($fn, $ln, $a){
$this->firstname = $fn;
$this->lastname = $ln;
$this->age = $a;

You are refering to an object from the class! :) (or attribute, depends how you look at it really)

450 points
Submitted by
over 3 years ago


0558c465783044be7bc75dd5c3d478c0?s=140&d=retro Mahmoud Khamis over 3 years ago

thank you very much ..... i got it .

5240e713abf821aa28001257_703076796 Robinvb over 3 years ago

No problem, glad to help you out, good luck!

0 votes


what exactly is it supposed to print? It's passing the code but printing 124


726 points
Submitted by
Sam Rawwsteen
over 3 years ago