Reputation: 5
I am trying to learn PHP OOP on codeacademy and I think I am going insane. I have compared my code with the example code in every single way and it just wont work! Please help me understand what is going wrong here, when i try to echo the age property from the object $student, made from the class Person
<?php
class Person {
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
public function __contruct($firstname, $lastname, $age)
{
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;
}
}
$teacher = new Person("boring", "12345", 12345);
$student = new Person('hans', 'hansen', 24);
$me = new Person('boring', '12345', 12345);
echo $student->age;
?>
Upvotes: 0
Views: 428
Reputation: 4650
Spelling mistake exist
class Person
{
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
//spelling mistake exist
public function __construct($firstname, $lastname, $age)
{
echo $firstname;
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;
echo 'i work';
}
}
$teacher = new Person("boring", "12345", 12345);
$student = new Person('hans', 'hansen', 24);
//print_r($student);
if(is_object($student))
{
echo $student->age;
}
$me = new Person('boring', '12345', 12345);
Upvotes: 0
Reputation: 3143
What is this ?
public function __costruct($firstname, $lastname, $age)
Check spelling costruct
- it must be construct
Upvotes: 1
Reputation: 1349
It is a grammar error... you wrote __contruct
try just replacing this line: public function __construct($firstname, $lastname, $age)
good luck learning PHP.
Upvotes: 0