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

Objects in PHP Forum

View Course » View Exercise

400 points
0558c465783044be7bc75dd5c3d478c0?s=140&d=retro
Submitted by
Mahmoud Khamis
almost 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

permalink

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
5240e713abf821aa28001257_703076796
Submitted by
Robinvb
almost 3 years ago

2 Comments

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

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

5240e713abf821aa28001257_703076796 Robinvb almost 3 years ago

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


0 votes

permalink

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

Thanks!

726 points
D8dfcd520f82f41c08939972112957df?s=140&d=retro
Submitted by
Sam Rawwsteen
almost 3 years ago