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

Objects in PHP Forum

View Course » View Exercise

260 points
3abed126072499d1c7c7c098b9ec3201?s=140&d=retro
Submitted by
Erin Kelly
about 2 years ago

8/10 Let's me pass, but doesn't work in display

Hello I just finished doing 8/10, well...it let me pass so kind of finish. But it didn't show up in the display. Is there any reason for that?

    <?php
    class Dog {
        public $numLegs = 4;
        public $name;

    public function __construct($name) {
        $this->name = $name;

        }

     public function bark() {
        return "<p>Woof!</p>";
        }

    public function greet() {
        return "<p>Hello, I am a talking dog named</p>" .$name.;
        }
    }

    $dog1 = Dog("Barker");
    $dog2 = Dog("Amigo");

    echo $dog2->greet();

    ?>

0 votes

permalink

Hi,

I noticed from your code that you are missing the '$this' syntax from within your greet function. It should be '$this->name' not '$name'.

also the exercise wants you to 'echo $dog1->bark();' too :)

573 points
5485fc1795e378709200044d_918218980
Submitted by
Adam
about 2 years ago


0 votes

permalink

Just noticed also when you are creating your instances it should be:

$dog1 = new Dog("Barker");
$dog2 = new Dog("Amigo");

573 points
5485fc1795e378709200044d_918218980
Submitted by
Adam
about 2 years ago


0 votes

permalink

Just remove your tag <p></p> on your code

and write your function greet like this:

 public function greet(){
        return "Hello, I am a talking dog named" .$this->name;
  }

1483 points
Picture
Submitted by
Ardi.Ws
about 2 years ago