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 trialLyndsie Anderson
1,874 PointsPHP Object Oriented Program 5/5 on Challenge
I can't seem to figure out why this code isn't passing the challenge. Does anyone see where I'm going wrong?
The goal is to return a string that uses $common_name, $flavor, and $record_weight
<?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;
}
public function getInfo() {
return "A " . $this->common_name . " is an " . $this->flavor . " flavored fish. The world record
weight is " . $this->record_weight;
}
$bass = new Fish("Largemouth Bass", "Excellent", "22 pounds 5 ounces");
}
?>
2 Answers
Greg Kaleka
39,021 PointsHi Lyndsie,
You're so close! The only thing you've done wrong is where you created your instance of Fish. This code is using the class you've created, and so it needs to be outside the class definition, after you've finished defining the class. All you need to do is move your last line of code outside the curly brace that closes the Fish class definition:
<?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;
}
public function getInfo() {
return "A " . $this->common_name . " is an " . $this->flavor . " flavored fish. The world record weight is " . $this->record_weight;
}
}
// We're done defining our Fish class. Now we can use it!
$bass = new Fish("Largemouth Bass", "Excellent", "22 pounds 5 ounces");
Ted Sumner
Courses Plus Student 17,967 PointsGreg is correct. I think if you had been a little more disciplined with your formatting you would have had an easier time spotting the issue. Look at Greg's formatting to see what I mean.