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

Objects in PHP Forum

View Course » View Exercise

408 points
4d25ed13034aef748edb0852e9815521?s=140&d=retro
Submitted by
clareh1977
over 3 years ago

Code is right but Parse error showing!

My code is right....I am allowed to proceed to the next lesson but I am getting this error in the pane on the right:
Parse error: syntax error, unexpected TSTRING, expecting TVARIABLE on line 15
(on line 7 here)

Code:
<?php
class Cat{
public $isAlive = true;
public $numLegs = 4;
public $name;

        public function__construct($name){
            $this name -> $name;
        }

        public function meow(){
            return "Meow Meow";
        }
    }
    $cat = new Cat("CodeCat");
    echo $cat->meow();
    ?>

1 vote

permalink

<?php
// Your code here
class Cat{
public $isAlive = true;
public $numLegs = 4;
public $name;
public function __construct($name){
$this->name= $name;
}
public function meow(){
return "Meow meow";
}
$cat1 = new Cat("CodeCat");
echo $cat1->meow();
}

    ?>

mine appears like this and i get the same error.

1521 points
571689776eb971c0fb0000ee_305660350
Submitted by
Dramos1981
almost 3 years ago


0 votes

permalink

The proper syntax for assigning a value to $this scope is: $this->name = $name;

450 points
Picture
Submitted by
Louis Milotte
over 3 years ago


0 votes

permalink

there are some mistakes in your code:
public function _construct($name){
//space between function and _
construct;
next there should be:
$this->name=$name;
next one:
return "Meow meow"; // second meow in lowercase;

871 points
Picture
Submitted by
Art K
over 3 years ago