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

Objects in PHP Forum

View Course » View Exercise

240 points
543359c0631fe9eb830007b6_989917941
Submitted by
CodeAlcazar
over 2 years ago

Hey, did you forget add my name as a property to the object? :-P

I tried many times, many forms, read question on the forum, but i can't pass this part, can someone help me?

<!DOCTYPE html>


Reconstructing the Person Class




<!-- Your code here -->
<?php
class Person{
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
}

        function __construct($firstname, $lastname, $age){
            $this->firstname = $firstname;
            $this->lastname = $lastname;
            $this->age = $age;
        }

        function greet(){
             return "hello, my name is" . $this->firstname . " " . $this->lastname  . ". Nice to meet you! :-)";   
        }
        $teacher = new Person("aburrida", "12345",12345);
        $student = new Person("aburrida", "12345",12345);
        echo $teacher->$isAlive;
        print $teacher->greet();
        print $student->greet();
    ?>
  </p>
</body>

Thanks for your time and attention.


4 votes

permalink

I figured the issue. You have to type "boring" exactly like that, not "BORING, Boring, or any other combination. This lesson is looking for that information to be exact or else it says it is incorrect.

Below is a snippet of that line:

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

And here is my now working code:
(NOTICE: I have added a bit that tells the age also just for practice, this can be included and still get a correct answer but is not necessary.)

    <?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! :-)";
        }
        public function tellage() {
           return $this->firstname . " is " . $this->age . " years old.";
        }
    }

        $teacher = new Person("boring", "12345", 12345);
        $student = new Person("Jeremy", "Polen", 24);

        echo $teacher->greet() . "<br />";
        echo $student->greet() . "<br />";

        echo $teacher -> tellage() . "<br />";
        echo $student -> tellage() . "<br />";
    ?>

648 points
524cc24c80ff3309cb006288_369387522
Submitted by
Jeremy Polen
over 2 years ago

1 Comment

543359c0631fe9eb830007b6_989917941 CodeAlcazar over 2 years ago

Sorry, hehe i didn't read this comment LoL


1 vote

permalink

closing parenthesis of class Person must be after function greet() , to simplify function __construct() and function greet() must be inside class Person

701 points
Picture
Submitted by
Dimik Jones
over 2 years ago

1 Comment

543359c0631fe9eb830007b6_989917941 CodeAlcazar over 2 years ago

I solve the problem thanks to all you, i wanna post the code but when i paste it, the code has no spaces between lines and it is hard to read, can you tell me how to post the code on a comment ? I'm new here, hehe


1 vote

permalink

I am getting the same error message back also. I have my greet() function and function __construct() in side of my class Person. My preview window seems to be displaying the correct info. I need help as well, can anyone lend a hand?

Here is my current 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! :-)";
        }
        public function tellage() {
           return $this->firstname . " is " . $this->age . " years old.";
        }
    }

        $teacher = new Person("Boring", "Adult", 55);
        $student = new Person("First", "Last", 24);

        echo $teacher->greet() . "<br />";
        echo $student->greet() . "<br />";
        echo $teacher -> tellage() . "<br />";
        echo $student -> tellage() . "<br />";
    ?>

648 points
524cc24c80ff3309cb006288_369387522
Submitted by
Jeremy Polen
over 2 years ago

1 Comment

543359c0631fe9eb830007b6_989917941 CodeAlcazar over 2 years ago

Try update the page, restore the code, paste it once more, and it has to solve your problem.