Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Object-Oriented PHP Basics (Retired) Inheritance, Interfaces, and Exceptions Final Challenge

hi, help

Create a new Trout object named $brook_trout with the following data: name: "Trout" flavor: "Delicious" record: "14 pounds 8 ounces" species: "Brook"

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;
    }

    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){
        $this->common_name = $name;
        $this->flavor = $flavor;
        $this->record_weight = $record; 
        $this->species = $species;
    }

  $brook_trout = new Trout() {

    $name = "Trout";
    $flavor = "Delicious";
    $record = "14 pounds 8 ounces";
    $species = "Brook";

  }
}


?>
Samuel Webb
Samuel Webb
25,370 Points

I would strongly suggest going through the videos again and getting a better grasp on this section before moving on.

Can you explain what problems you're having? What's confusing you?

Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,413 Points

Hi Raul,

I got stuck on this too at first and had to ask for help.

This should work:

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

2 Answers

Sam Donald
Sam Donald
36,305 Points

Hi Raul,

You've created a Trout class and assigned a set of 4 excepted/expected values

$name, $flavor, $record, $species

Now you are creating an Object of the Trout class. You can think of this as a subset of Trout. That is to say that a Brook Trout is a type of Trout and so it inherits values common among Trout. You might also say that a Toyota Corolla is a type of Toyota so it shares similarities with other Toyota's. Like, Manufacturer, Badge, Country of Origin etc...

Because your $brook_trout is a subset of Trout (i.e.

$brook_trout = new Trout

When you pass in the 4 common values as Juliette Tworsey notes above, the Trout class assigns them to

$name, $flavor, $record, $species
Juliette Tworsey
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Juliette Tworsey
Front End Web Development Techdegree Graduate 32,413 Points

Thanks for the well thought out explanation Sam. I haven't seen this code in 8 months and now I feel like I have a more thorough understanding of it the 2nd time around.

i don't understand what the question wants