public function __conttruct($firstname, $lastname, $age){
That's not how you spell __construct.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In my effort to learn OOP I'm trying to figure this out and maybe someone can help. Yea I know this is basic stuff, but what can I say.
I'm tying to figure out why I get this error Undefined variable: age (line 27) Fatal error: Cannot access empty property on line 27
Code
<!DOCTYPE html>
<html>
<head>
<title>Reconstructing the Person Class</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<p>
<?php
class person {
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
public function __conttruct($firstname, $lastname, $age){
$this->$firstname = $firstname;
$this->$lastname = $lastname;
$this->$age = $age;
}
}
$teacher = new Person("boring", "12345", 12345);
$student = new Person("John", "Doe", 34);
echo $student->$age;
echo $teacher->isAlive;
?>
</p>
</body>
</html>```
So doesn't the constructor pull the values $student variable?
Don't laugh. LOL
Please or to participate in this conversation.