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) Inheritance, Interfaces, and Exceptions Final Challenge

Jeffrey Cunningham
Jeffrey Cunningham
5,592 Points

Help me complete the fish challenge please.

I'm at the part where it says to "Add a getinfo method to your Trout class that returns a string containing the common_name. flavor, record_weight, and species properties."

It wont let me pass. I think it has something to do with how I am concatenating the string.

Here is my code:

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() {
        $output  = "The {$this->common_name} is an awesome fish. ";
        $output .= "It is very {$this->flavor} when eaten. ";
        $output .= "Currently the world record {$this->common_name} weighed {$this->record_weight}.";
        return $output;
    }
}

class Trout extends Fish 
{

  public $species;

  function __construct($name, $flavor, $record, $species) {
    parent::__construct($name, $flavor, $record);
     $this->species = $species;

  }

    public function getinfo(){
      return $species . $name . " tastes " . $flavor . " The record held is " . $record; 
    }
}

$brook_trout = new Trout("Trout", "Delicious", "14 pounds 8 ounces", "Brook");

?>```

1 Answer

Hi Jeffrey, I put your code into a workspace and played around with it for a bit.

What's happening is the getInfo() method is returning the construct function argument names ($species, $name, $flavor, $record). But those arguments are only accessible/usable from INSIDE the construct function. The construct functions are assigning values to the public properties of the Fish and Trout classes (i.e., $common_name, $flavor, $record_weight, $species).

So when you created your $brook_trout object instance at the bottom, you were assigning values to those class properties. And because Trout is an extension of Fish, it then inherits all of the public properties of Fish. So the way you access those properties is by using $this. So $species becomes $this->species; $name changes to $this->common_name; $flavor to $this->flavor; and $record to $this->record_weight. See the code block below. That seemed to work for me when I tested it out, so with that change you SHOULD pass the challenge.

public function getinfo(){
      return $this->species . " " . $this->common_name . " tastes " . $this->flavor . ". The record held is " . $this->record_weight; 
    }
Jeffrey Cunningham
Jeffrey Cunningham
5,592 Points

Thank you so much! I'm still trying to wrap my head around exactly what just happened there but it worked. I was stuck on that for weeks!