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

Objects in PHP Forum

View Course » View Exercise

121 points
D65b7b91e21dcf24d72cdbc10e8ba485?s=140&d=retro
Submitted by
mykelis
almost 3 years ago

Objects in PHP 5/10

Can't figure out what I am doing wrong. Shows unexpected syntax '}" on line 22. Any pointers?

<?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 . "and I am" . $this->age . "years old"
}

    }

    $teacher = new Person("boring","12345",12345);
    $student = new Person("outgoing","19",19);
    echo $subj1->teacher;
    echo $subj2->student;
    echo $teacher->greet();
    echo $student->greet();
    ?>

1 vote

permalink

Seems as you opened "{" twice and only closed it once.

"class Person { " and "public function _construct($firstname,$lastname,$age){" and only closed it at "$this->age = $age; }"

I used the following code which was correct:
<?php
class Person {
$teacher = new person ("boring", "12345", "12345");
$student = new person ("Hello", "Bye", "12");
public $isAlive = true;
public $firstname = ;
public $lastname = ;
public $age = ;
}
echo $teacher $isAlive;
echo $student $age;

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

285 points
F98324591d1a7145f029175e77a32b66?s=140&d=retro
Submitted by
David Sidhu
almost 3 years ago

1 Comment

D65b7b91e21dcf24d72cdbc10e8ba485?s=140&d=retro mykelis almost 3 years ago

Thanks : )


1 vote

permalink

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;

        }
        }


        $teacher = new Person("boring", "12345", 12345);
        $student = new Person("Vasile","Tonofa",19);


        echo $student->age;

485 points
53f724679c4e9d5562000c73_4189007
Submitted by
friol240
over 2 years ago