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

please :D

Add a property named species to the Trout class.

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

  $species = new Fish();
}

?>

3 Answers

Samuel Webb
Samuel Webb
25,370 Points

You're not adding the property correctly. A property would be added in the same way the Fish class adds the common_name, flavor and record_weight properties. Just in the Trout class.

I don't understand :D

Samuel Webb
Samuel Webb
25,370 Points

It might be useful to re-watch the video that discusses adding properties to classes again. Sometimes it's really helpful to check out some of the videos multiple times until you have a more solid understanding of what's being taught. PHP becomes really difficult, really quickly and without a good grasp on the basics, you'll end up lost.

But to answer your question:

class Trout extends Fish{
    public $species;

    function __construct($name, $flavor, $record){
        $this->common_name = $name;
        $this->flavor = $flavor;
        $this->record_weight = $record; 
    }
}

Edit: Fixed small typo.

it dosen't work

Samuel Webb
Samuel Webb
25,370 Points

Oops, I added the dollar sign before public. It's supposed to be before species.

public $species;

thanks, man!