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

Objects in PHP Forum

View Course » View Exercise

660 points
51dd3a3d3c627370486df0d52bf4f4b6?s=140&d=retro
Submitted by
bitSurfer71926_53d9b224c660e4dda6000
almost 3 years ago

greet() echoes string without "Amigo" name; still passes?

class Dog{
            public $numLegs = 4;
            public $name;

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

            public function bark(){
                return "Woof!";
            }

            public function greet(){
                return "Hello, I am dog but call me " . $this->name . ".";
            }
        }

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

        echo $dog1->bark();
        echo $dog2->greet();

4 votes

permalink

Hi, check this one, it's working OK, and as mentioned by khalid92 and Jim Tryon, use use _construct, not _constructor:

<?php
            class Dog {
                public $numLegs = 4;
                public $name;
                public function __construct($name) {
                    $this->name = $name;
                }
                public function bark() {
                    return "Woof.";
                }
                public function greet() {
                    return " Hi ". $this->name . " it's all good!";
                }
            }
            $dog1 = new Dog("Barker");
            $dog2 = new Dog("Amigo");
            echo $dog1->bark();
            echo $dog2->greet();
        ?>

905 points
54117b089c4e9da126001402_155391577
Submitted by
virtualarrow
almost 3 years ago


1 vote

permalink

Exactly the same problem.

1270 points
Picture
Submitted by
Milijana Ristić
almost 3 years ago


1 vote

permalink

Only make public function _construct not _constructor

768 points
52b5acf19c4e9d9f50001991_389654467
Submitted by
khalid92
almost 3 years ago


1 vote

permalink

Yes, it is because of the name you gave to the constructor.

In PHP, use __construct

130 points
Df985d7d832006621a8c634ceda9489d?s=140&d=retro
Submitted by
Jim Tryon
almost 3 years ago