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

Objects in PHP Forum

View Course » View Exercise

1115 points
5198f52c32450b2ccd000e3d_989129517
Submitted by
Nalin Natrajan
about 3 years ago

Why do I keep getting this message: Oops, try again. Did you add a 'greet' method?

I cannot seem to get this code to pass even though the output seems correct. Any assistance would be great!

<!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 __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("tired", "54321", 54321);
                $student = new Person("your", "name", 99);
                echo $teacher->greet();
                echo $student->greet();
        ?>
      </p>
    </body>
</html>

0 votes

permalink

I got this error (Oops, try again. Hey, did you forget add my name as a property to the object? :-P).. i cant pass

469 points
B3a273ea4c5dafd5188f0d7eb8127e75?s=140&d=retro
Submitted by
997804939
about 3 years ago


0 votes

permalink

here's working code. hope it helps.. i also tried yours and it worked for me… just try refreshing your page.. sometimes i even copy and then refresh the page and then paste the code back in.. might do the trick..

<!DOCTYPE html>


Reconstructing the Person Class




<?php
class Person {
public $isAlive = true;
public $firstname;
public $lastname;
public $age;

public function __construct($firstname, $lastname, $age) {
        $firstname->prop1 = $jon ;
        $lastname->prop2 = $smith ;
        $age->prop3 = 26 ;
        }

        public function greet() {
            return "Hello, my name is " .$this->firstname . "" .$this->lastname . ". Nice to meet you! :-)"
        }
    }
       $obj1 = new $teacher("boring", "12345", 12345);
        $obj2 = new $student("Jon", "Smith", 26);
        echo $teacher->greet();
        echo $student->greet(); 

    ?>
  </p>
</body>

988 points
532c936a631fe90cf4000e86_809729952
Submitted by
sezobel
about 3 years ago