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

Objects in PHP Forum

View Course » View Exercise

397 points
99633780906eeca79c16c15696c43809?s=140&d=retro
Submitted by
jaymutzafi
over 4 years ago

Output not as expected

Hi,

In this lesson: http://www.codecademy.com/courses/web-beginner-en-ZQQ64/0/6# 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:

<?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("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,
Jay


1 vote

permalink

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

3108 points
5154c6cf115b457ac9000021_667301409
Submitted by
boring12345
over 4 years ago

2 Comments

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! :-)