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

Objects in PHP Forum

View Course » View Exercise

311 points
711529bebeae041d09f5efaecd1b2c9a?s=140&d=retro
Submitted by
PiscesGamer
over 2 years ago

The code passes but is still doesn't print anything out

The course allows me to pass but the actual code is gving me an error.

The error:
Parse error: syntax error, unexpected T_PUBLIC on line 21

<!DOCTYPE html>
<html>
    <head>
      <title>Reconstructing the Person Class</title>
      <link type='text/css' rel='stylesheet' href='style.css'/>
    </head>
    <body>
      <p>
        <?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('Andy', 'Oppenheimer', 16);

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

        ?>
      </p>
    </body>
</html>

0 votes

permalink

You forgot the closing '}' for the constructor function and had an extra '}' after the end of the class definition
...
$this->age = $age;
}
public function greet() {
return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
}
}
$teacher =

134 points
10622d072c3aaa04ad7c84286cfbe98d?s=140&d=retro
Submitted by
demediaMes
about 2 years ago

2 Comments

711529bebeae041d09f5efaecd1b2c9a?s=140&d=retro PiscesGamer about 2 years ago

Thank You for answering. I eventually realised that I had accidentaly place the greet() function isnide the __constructor().

55a01fd5e39efe4ab700008d_693488337 JollyJuggler02 almost 2 years ago

Yes. Before you post codes for help, make sure that there are no typos within the code. I saw a couple.