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

Objects in PHP Forum

View Course » View Exercise

595 points
Picture
Submitted by
Chip Eyler
about 4 years ago

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE

<?php
    class Dog{
        public $numLegs = 4;
        public $name;
        public function__construct($name) { 
            $this->name = $name;
        }
    }
    ?>

I keep getting the parse error msg in the output for the line "public function__construct($name) { "

Any ideas?


6 votes

permalink

<?php
class Dog {
public $numLegs = 4;
public $name;
public function __construct($name){
$this->name = $name;
}
}
?>

554 points
519a2e4a05dfa31a36003047_35877524
Submitted by
Joseph
about 4 years ago

2 Comments

53f6364a631fe968a80000c9_804808581 calebbradford almost 3 years ago

it's not working

53f6364a631fe968a80000c9_804808581 calebbradford almost 3 years ago

it worked sorry


1 vote

permalink

Keep them apart!
function_construct
function _
construct

1142 points
B1b65b0bd5115c19d9294c917a8444ac?s=140&d=retro
Submitted by
Kuofp
about 4 years ago


1 vote

permalink

  function+space+2 underscores+construct

  function __construct

  I think

683 points
67cea673ddb964518ae6c5764366ab59?s=140&d=retro
Submitted by
seb204l
about 4 years ago


0 votes

permalink

<?php
class Dog {
public $numLegs = 4;
public $name;
public function __construct($name){
$this->name = $name;
}
}
?>

554 points
519a2e4a05dfa31a36003047_35877524
Submitted by
Joseph
about 4 years ago


-2 votes

permalink

What am I doing wrong here? Same error for the line with " this->firstname = $firstname;"

<?php
    class Person {
       public $isAlive = true;
       public $firstname;
       public $lastname;
       public $age;
       public function __construct($isAlive, $firstname, $lastname, $age){
           this->firstname = $firstname;
           this->lastname = $lastname;
           this->age = $age;
       }
    }
    $teacher = new Person("boring", "12345", 12345);
    $student = new Person("Chip", "Eyler", 35);
    echo $student -> age;
    ?>

595 points
Picture
Submitted by
Chip Eyler
about 4 years ago

1 Comment

B1b65b0bd5115c19d9294c917a8444ac?s=140&d=retro Kuofp about 4 years ago

Here it is!
$this->firstname = $firstname;

There is no "$" when you access a property in a class, but this object needs a "$".