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

PHP Object-Oriented PHP Basics (Retired) Properties and Methods Mid-Course Challenge

Alessandro Giordo
Alessandro Giordo
2,065 Points

Error in getInfo(); don't understand

I have the code below and it gives me error on line17. Why is that?

fish.php
<?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;
  }

}
$bass = new Fish ("Largemouth Bass", "Excellent", "22 pounds 5 ounces");

echo $bass->getInfo();
?>

3 Answers

Seth Reece
Seth Reece
32,867 Points

Hi Allessandro,

This challenge is asking for you to create a method called getInfo() on the Fish class that returns a string. You should create a function inside the Fish class, and return a concatenated string using $this->$public_variable. e.g.

<?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");

?>
Alessandro Giordo
Alessandro Giordo
2,065 Points

Hi guys thanks for the answer. I suppose this includes a previous understanding of php that I don't have as that's not covered in the videos of that course. Can you suggest a track for me please?

Seth Reece
Seth Reece
32,867 Points

It's less a previous understanding of PHP, and more an understanding of scripting languages in general for me. You will find in scripting language courses that the challenges often ask you to do things using the concepts taught. They tend to focus less on memorization. It's really just a matter of practice. You do qualify for extra credit, since you did correctly echo out $bass->getINfo() which would be the next step if there was one.

Alessandro Giordo
Alessandro Giordo
2,065 Points

Thanks everyone I understand the issue and solved the problem :)