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

Objects in PHP Forum

View Course » View Exercise

397 points
Submitted by
over 4 years ago

Output not as expected


In this lesson: I have written the following code and submitted it and got a successful message BUT it displayed the output without the names. I wanted to understand why it happened, what did I do wrong?

Here is the code:

        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("Jay", "M", 32);

        echo $student->greet();
        echo $teacher->greet();

The output in the preview screen was this:

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

As you can see, this does not include the names. how come?

Thanks in advance,

1 vote


your spelling is inconsistent:
Sometimes you write $firstname and sometimes you write $firstName. ;-)

3108 points
Submitted by
over 4 years ago


99633780906eeca79c16c15696c43809?s=140&d=retro jaymutzafi over 4 years ago

Oh damn! I feared it was something silly like that. but after scanning it several times I gave up. Thanks!

5154c6cf115b457ac9000021_667301409 boring12345 over 4 years ago

You're welcome! :-)