Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dk4210's avatar

OOP Question

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
0 likes
6 replies
36864's avatar
public function __conttruct($firstname, $lastname, $age){

That's not how you spell __construct.

daniel-banciulea's avatar

And you have an extra $ sign on:

echo $student->$age;

should be:

echo $student->age;
dk4210's avatar

I dont get the error now but dont get the age

<!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 __construct($firstname, $lastname, $age){
                $this->$firstname = $firstname;
                $this->$lastname =  $lastname;
                $this->$age = $age;
            }
           
           }
          
          $teacher = new Person("boring", "12345", 12345);
          $student = new Person("dan", "Kelly", 47);
          
          echo $student->age;
         //echo $teacher->isAlive;
        ?>
      </p>
    </body>
</html>
36864's avatar

You should read through the php documentation for variables http://php.net/manual/en/language.variables.variable.php

$this->$firstname = $firstname; is basically saying "evaluate the value of $firstname as the name of a property, and assign the value of $firstname to it. If the value of $firstname is "Steve", this is the same as $this->Steve = 'Steve'

$this->firstname = $firstname is how you assign a value to the "firstname" property.

dk4210's avatar

I see now. Thanks so much for the help 36864. You're awesome!

Please or to participate in this conversation.