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 trialdeepsabey
2,295 Pointsobject oriented PHP basics step 5 of challenge 5
not clear how to write the public function getInfo () section. please help. code is attached. thanks in advance
<?php
class Fish {
public $common_name = 'common_name';
public $flavor = ' flavor';
public $record_weight = 'record_weight';
function __construct ( $name, $flavor, $record) {
$this-> common_name =$name;
$this-> flavor = $flavor;
$this -> record_weight = $record;
}
public function getInfo () {
return "comon_name". $this->a common_name is an ;
return "flavor".$this-> flavor flavored fish.;
return "record_weight".$this-> The world record weight is record weight;
}
}
$bass = new Fish("Largemouth Bass", "Excellent", "22 pounds 5 ounces" );
echo $bass ->getInfo();
?>
5 Answers
Jason Anders
Treehouse Moderator 145,860 PointsHi there,
If you made it to the 5th task, you were doing okay, but I'm not sure where you are going with the 5th. First, I don't know where you are getting the term "public function," but I think that may be what is causing you some confusion.
The challenge asks "Create a method on Fish named getInfo that takes no parameters and returns a string that includes the common_name, flavor, and record_weight for the fish. When called on $bass, getInfo might return "A Largemouth Bass is an Excellent flavored fish. The world record weight is 22 pounds 5 ounces."
The word public is nowhere in there?
Anyways, you've already created a method (a function is called a method inside of a Class) called "__construct," now you just need to create one called "getInfo" that will return the stated string. And you need to delete the echo
statement at the end, because the challenge did not ask for it... it just asks for you to create the method and return the string.
I think you may have just been over thinking the final part. The complete and corrected code is below.
<?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;
}
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')
?>
I hope this helps and makes sense. Keep Coding! :)
michaelmugo
3,971 PointsI've been having the same problem and tried this code that you recommended, but it stil fails giving the following in the preview:
A Phoney Baloney is an subpar flavored fish. The world record weight is 1200 pounds, 296 ounces.[[CCENGINE 33345629-8f1c-45bf-b97e-e835f868106f fail:5:Be sure you include the fish's common name in the return value from getInfo. Use $this to reference the current object.]]
Oddly enough it works locally on my computer and gives the answer required when the following is called '''html <?php $bass->getInfo(); '''
Jason Anders
Treehouse Moderator 145,860 PointsHi michaelmugo.
If the name/weight, etc that I'm reading in your error is what you are using, that is why it is failing the challenge. Treehouse challenges are very specific and very picky. You need to use exactly what the challenge asks for. If you change the name of a variable/value, change a Capital to lower-case, or even miss a period or exclamation mark, you will get the "Bummer!"
The code above does pass the 5th challenge. Just make sure you are using the values the challenge wants.
:)
michaelmugo
3,971 PointsI realised that I was echoing from the getInfo() function instead of returning.
So returning instead fixed my problem. Thanks for your help!
Shannon Beach
32,353 PointsI've had to much JS lately, I was trying to do string concatenation lol. This is what I used
function getInfo() { return "A .$this->common_name is an .$this->flavor flavored fish. The world record weight is .$this->record_weight"; }
Ted Sumner
Courses Plus Student 17,967 PointsJason Anders is mostly correct. You should use public function getInfo()
because you should always expressly state whether methods or attributes are public, protected, or private.
With respect to your getInfo(), you can only have one return. If you wanted to do something like you did, you need to do it with a variable and return the variable.
Gavin Mace
30,747 PointsKinda confusing when Ted Sumner and Jason Anders are both Mods and give different takes on what the correct answer is.
Given 'public' is not requested and if nothing is entered, it is public anyway, then I don't see why you need to specify public.
Jason Anders
Treehouse Moderator 145,860 PointsHey Gavin Mace,
You are correct, public is not asked for by the challenge and, in my opinion, is not needed in that code. However, Ted is also right...
Welcome to coding. :)
You will find, as you progress, that there are often MANY "right" ways to do/accomplish the same thing. One way is not always better than another, and the important things is to stay consistent in how you do it. As long is it's D.R.Y. and works... and if you are working in a group, consistent to 'group norms', then you are doing it right. There really is no "right" and "wrong" in the world of code.
Keep Coding! :)
Gavin Mace
30,747 PointsThanks Jason,
Might be a fairly basic question, but I guess I'll ask since it's confusing me....
In the last handful of tutorials, the PHP tags aren't closed, but the code's still working. There's always a <?php open tag, but never a ?> close tag. Any reason for that?
Jason Anders
Treehouse Moderator 145,860 PointsHey Gavin,
Yeah, that's another one of those do/don't can/can't type of things. If your code is only PHP, it is actually recommended that you do not use a closing ?> as unintended whitespace after can cause some issues.
Here are a few links that kind of explain it better than I:
I hope these help. :)
Siim Kallari
3,401 PointsSiim Kallari
3,401 PointsYou can make a variable and then add the info you need to it, then return the variable with information.
For example: