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

Objects in PHP Forum

View Course » View Exercise

449 points
Cd5dad41e9fe4bd71c62f70673dd560a?s=140&d=retro
Submitted by
Jhon Mario Lemos Vélez
over 3 years ago

Property Panic (2) 5/10

I don´t understand, what is the problem here.

<!DOCTYPE html>


Reconstructing the Person Class




<!-- Your code here -->
<?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 . $this->age=$age;
       }
    }

    $firstname = new person("boring");
    $lastname = new person("12345");
    $age = new person ("12345"); 
    echo $age->age;
    ?>
  </p>
</body>

1 vote

permalink

Thank you,
now, this is my code but i get this message.
Oops, intentalo otra vez. Hey, did you forget add my name as a property to the object? :-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;
       }
       public function greet() {
         return "Son " . $this->firstname . " " . $this->lastname . " " . $this->age; 
       }

    }
    $teacher = new person ("jhon", "lemos", "38");

    echo $teacher->greet();

    ?>

449 points
Cd5dad41e9fe4bd71c62f70673dd560a?s=140&d=retro
Submitted by
Jhon Mario Lemos Vélez
over 3 years ago

1 Comment

Picture Tom over 3 years ago

$teacher = new Person("boring","12345",12345);


0 votes

permalink

When you call a constructor, you need to pass it ALL the parameters it is expecting. Your code is calling the constructor THREE times, each time creating a new person object, one called $firstname with only one parameter, another called $lastname, also with only one parameter, and a third person called $age, with only one parameter.

And in your greet function ( the greet method of the Person object), you have a typo at the end of the return line, probably the result of a copy and paste -- get rid of the "=$age" at the end of that line.

572 points
52f1d2e39c4e9dbb8d000250_986039286
Submitted by
Byron Canfield
over 3 years ago


0 votes

permalink

What name is it expecting you to add? Looks to me like your revised code is still not following the instructions.

572 points
52f1d2e39c4e9dbb8d000250_986039286
Submitted by
Byron Canfield
over 3 years ago


0 votes

permalink

The code checker is just not very bright. Its expecting the teachers name to be "boring" and anything else (including "Boring" or "BORING" ) will be rejected.

659 points
8a3e6b18ac988b23eeaea7dc660027da?s=140&d=retro
Submitted by
antoniosf
over 3 years ago

1 Comment

52f1d2e39c4e9dbb8d000250_986039286 Byron Canfield over 3 years ago

Well, in some cases, yes, the code checker is not very bright, but in this case, there is no ambiguity in the instruction to use specific data.