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

Objects in PHP Forum

View Course » View Exercise

633 points
344085583c4a27400c22c6df4996eb17?s=140&d=retro
Submitted by
Ryan Shaw
over 2 years ago

Getting: Hello, my name is . Nice to meet you! :-) Hello, my name is . Nice to meet you! :-)

I keep getting a dot in place of where the names should be. Here is my code:

       <?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; } 
         public function greet() {
              return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-) ";

             }
      }
      $teacher = new Person("boring", "12345", 12345);
      $student = new Person("Blank", "Blank", 20);
      echo $student->greet();
      echo $teacher->greet();

    ?>
    ```

3 votes

permalink

{
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;
}

The dollar signs are not suppose to be next to the actual properties that you would call later.

That's all I saw. I'm not sure what you mean by dot in place of where your name should be
I didn't text your code. I just looked at what mine looks like to yours.

1435 points
553fa4d6e39efe6b62000498_671622430
Submitted by
Bennie Dewaal
over 2 years ago


0 votes

permalink

Same problem. Here's my code:

<?php
    class Person {
        public $isAlive = true;
        public $firstname;
        public $lastname;
        public $age;
        public function __constructor($firstname, $lastname, $age) {
            $this->firstname = $firstname;
            $this->lastname = $lastname;
            $this->age = $age;
        }
        public function greet() {
            return "Hello, my name is".$this->firstname." ".$this->lastname."                          .Nice to meet you! :-)";
        }


    }
    $teacher = new Person("boring", "12345", 12345);
    $student = new Person("Gasper", "Kreft", 14);
    echo $teacher->greet();
    echo $student->greet();

    ?>

993 points
36a1e49557d279e27d418f7cf98d2740?s=140&d=retro
Submitted by
Gašper Kreft
over 2 years ago

1 Comment

36a1e49557d279e27d418f7cf98d2740?s=140&d=retro Gašper Kreft over 2 years ago

Oh, I just found my problem. I typed _constructor instead of _construct.