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
about 2 years ago

whats wrong with my code?

I keep getting "Undefined variable: name (line 17)hello my name is"

here is 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()


   ?>

0 votes

permalink

You just forgot '$this->name' in the 'greet' function.
ie
public function greet(){
return "hello my name is ". $this->name;
}

134 points
10622d072c3aaa04ad7c84286cfbe98d?s=140&d=retro
Submitted by
demediaMes
about 2 years ago


0 votes

permalink

exercise 4/10, I have this syntax :

   <? php




    class person {
        public $isAlive=true;
        public $firstname;
        public $lastname;
        public $age;
    }


    class person {

        $obj1=new person ($teacher);
        $obj2=new person ($student);

    }
    echo $obj1 ->"is alive";


?>

but I keep getting the message "oops, try again ! I don't see the php tags anymore" WTF ?

764 points
Picture
Submitted by
Steve Wilkins
about 2 years ago