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 trial
Jonathan Grieve
Treehouse Moderator 91,254 PointsShould this not echo something to the screen?
Just finished the Properties and Methods Stage of the brilliant PHP Object Oriented course.
After finishing the stage I decided to have a go myself.
First by putting it into localhost on XAMPP and then here on my server. Warning: link may not work some time after question is answered, :)
Here's the code
<?php
class Fish {
//properties
public $common_name;
public $flavor;
public $record_weight;
//constructor method - 3 parameters
function __construct($name, $flavor, $record){
//assigned properties with the 3 parameters
$this->common_name = $name;
$this->flavor = $flavor;
$this->record_weight = $record;
}
//A method that returns a string wuth property values
function getInfo() {
return "A " . $this->common_name . " is an " . $this->flavor . " flavoured fish. The world record weight is " . $this->record_weight . ".";
}
}
$bass = new Fish("Largemouth Bass","Excellent","22 pounds 5 ounces");
echo $bass;
?>
Shouldn't I see something be echoed to the screen below the word testing? Even if I just use the return keyword to try and display the sentence?
Thanks. :)
3 Answers
Jake Lundberg
13,965 PointsNo, you will not see anything because you are passing echo the entire object itself. If you want it to print something, you would need to pass echo something like the results of your object's getInfo method.
Hope this helps!
Kevin Korte
28,149 PointsJake is correct, so plus one. var_dump($bass) instead of echo to see your object on the screen.
Jonathan Grieve
Treehouse Moderator 91,254 PointsThanks, I figured out how to get the sentence displayed to the screen with the Object operator based on this and info from another thread. :)
<?php
$bass = new Fish("Largemouth Bass","Excellent","22 pounds 5 ounces");
echo $bass->getInfo()
?>