Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCatherine Lalonde
9,193 PointsChallenge task 3 of 5
This is a question for:
In the new constructor method, assign each of the properties on the Fish class with its corresponding parameter variable.
I have played around with it, and the error it is giving me is:
Bummer! Be sure the $name parameter is assigned to the common_name property.
I have public $common_name = 'name'; with __construct( $name, and $this->name = $common_name; .
I do not know if I have something backwards or not... but the task will not validate.
class Fish {
public $common_name = 'name';
public $flavor = 'flavor';
public $record_weight = 0;
public function __construct( $name, $flavor, $record){
$this->name = $common_name;
$this->flavor = $flavor;
$this->record = $record_weight;
}
public function getInfo(){
return "Fish:". $this->name;
}
}
$haddock = new Fish('Haddock','Plain','6');
Still nothing and I even shortened it down to just:
class Fish {
public $common_name = '';
public $flavor = '';
public $record_weight = '';
public function __construct($name, $flavor, $record){
$this->name = $common_name;
}
}
5 Answers
Joakim Bergman
14,501 PointsYou need to change the places of $name and $common_name in the construct method. You're supposed to be assigning the argument $name to $this->common_name and not the other way around, so it should be $this->common_name = $name.
David Endersby
24,915 PointsFinally figured it out
<?php
class Fish {
public $common_name ='';
public $flavor='';
public $record_weight='';
function __construct($name, $flavor, $record){
$this-> common_name = $name;
$this -> flavor = $flavor;
$this -> record_weight = $record;
}
}
?>```
Catherine Lalonde
9,193 Points^_^ OMG thank you I knew I had to be mixing something up.
Rilwan Shinaba
Courses Plus Student 130 Pointswere did i go wrong class Fish {
public $common_name ; public $flavor ; public $record_weight ;
public function __construct( $name, $flavor, $record){ $this->$common_name = $name ; $this->$flavor = $flavor; $this->$record_weight = $record ; }
}
ilanaguttman
2,700 PointsYou need to add $common_name = something. Same for the other objects.
amadeo brands
15,375 PointsHard one.
I got it working with this code:
<?php
class Fish {
public $common_name;
public $flavor;
public $record_weight;
function __construct(
$name,
$flavor,
$record
) {
$this-> common_name =$name;
$this-> flavor =$flavor;
$this-> record_weight =$record;
}
}
?>
Spenser Hale
20,915 PointsSpenser Hale
20,915 PointsThank You, I did same thing