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

Objects in PHP Forum

View Course » View Exercise

620 points
E85fc16dec5dfaf8ba5883cd88574f6e?s=140&d=retro
Submitted by
Alessandro Lagasca
about 2 years ago

6/10 I pass the exercize but i get a fatal error :\

Hi all! i was doing the 6/10 but the system is telling me that there is a fatal error on line 26, when i echo teacher :) What to you think about? why does it didn't print out the sentence?

 <?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("Alessandro", "Ganga", 28);
    echo $teacher->greet();
    echo $student->greet();
   ?>

i get: Fatal error: Call to undefined method Person::greet() on line 26

Thank u for the support :)


2 votes

permalink

hello try this. it worked for me.

<!DOCTYPE html>


Reconstructing the Person Class




<?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("Rohini", "Singh", 23);
echo $teacher->greet();
echo $student->greet();
?>

254 points
8956207c396ab97e402eca36c2a48e07?s=140&d=retro
Submitted by
Rohini Singh
about 2 years ago


0 votes

permalink

Thank you, now i get it! i put the double underscore before greet, but it's probably useless and it gets an error :) Thank u!

620 points
E85fc16dec5dfaf8ba5883cd88574f6e?s=140&d=retro
Submitted by
Alessandro Lagasca
about 2 years ago


0 votes

permalink

<!DOCTYPE html>


Reconstructing the Person Class




<!-- Your code here -->
<?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("samuel", "akosile", 15);

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


    ?>


  </p>
</body>

501 points
55dc829a86f552a56a0001f2_892706734
Submitted by
Akosile Samuel Ilemobayo
about 2 years ago