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 trialFat Dog
18,848 PointsAbout the QUIZ
This is the result when I'm previewing my code: PHP Parse error: syntax error, unexpected 'public' (T_PUBLIC) in fish.php on line 13 PHP Parse error: syntax error, unexpected 'public' (T_PUBLIC) in fish.php on line 13
What's wrong with my 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;
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");
?>
3 Answers
Ted Sumner
Courses Plus Student 17,967 PointsYou close your constructor after your getInfo(). Close the constructor, then start getInfo() and you should be good. My quick glance did not see anything else.
inakicalvo
23,166 PointsHi there,
Your getInfo function is messed up. You should have concatenated strings and variables, but you treatead the whole thing as if it was a string.
Besides, you didn't call the function at the end.
Compare your code to mine to see what I mean:
<?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");
echo $bass->getInfo;
?>
I hope you find it helpful.
Cheers!
Ted Sumner
Courses Plus Student 17,967 PointsHis return is actually fine. When you use double quotes the variables are parsed prior to the echo. If you use single quotes, you need to concatenate as you do in your example. It was not explained well on Treehouse and I did what you did for a long time before I found out. It is very helpful.
<?php
$name = 'Ted';
echo 'My name is $name.';
// Returns: My name is $name.
echo "My name is $name.";
// Returns: My name is Ted.
Ted Sumner
Courses Plus Student 17,967 PointsYou are right on the echo $bass->getInfo();
but I am not sure he is to that step on the challenge.
Fat Dog
18,848 PointsThank you Ted, all I need is to close the constructor function with the brackets.