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

Objects in PHP Forum

View Course » View Exercise

338 points
858beaefff96303788a5511094161aac?s=140&d=retro
Submitted by
nachlop
almost 3 years ago

whats wrong with my code?

   <?php
   class Dog {
       public $numLegs = 4;
       public $name;
       public function bark (){
           return "woof!";
       }
       public function greet(){
           return "hello my name is". $name;
       }
       public function __construct($name){
           $this -> name = $name;
       }

   }

   $dog1 = new Dog('barker');
   $dog2 = new Dog ('Amigo');

   echo $dog1 -> greet()


   ?>
  </p>
</body>

0 votes

permalink

My Code:

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

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

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

        public function greet() {
            return 'Hello, my name is ' . $this->name . '.';
        }
    };

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

    echo $dog1->bark() . '<br>';
    echo $dog2->greet();
?>

2283 points
A86f7a545bb298e9d17b1b0ba5ae2a31?s=140&d=retro
Submitted by
Sergeant Shift
over 2 years ago