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

Robbie Thomas
Robbie Thomas
31,093 Points

Code Challenge 7/7

What am I doing wrong?

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){
      parent::__construct($name, $flavor, $record);
      $this->species = $species; 
   }
      function getInfo() {
       return $this->species $this->common_name . " tastes " $this->flavor. "The record " $this->species $this->common_name $this->record_weight;
}

$brook_trout = new trout("Trout", "Delicious", "14 pounds 8 ounces", "Brook");
echo $brook_trout->getInfo();

?>

5 Answers

I see several things. Remember PHP is case sensitive. You create Fish, but extend fish. You do not concatenate $this->species and $this->common_name in your return. Actually, you are missing most of your concatenation. If I remember right, this code passes:

<?php
return "$this->species $this->common_name tastes $this->flavor.  The record $this->species $this->common_name is $this->record_weight.";

I do not use periods to concatenate because PHP will evaluate the variables when enclosed with a double quote. It will not work with single quotes. You can also do this:

<?php
return $this->species . $this->common_name . ' tastes ' . $this->flavor . '.  The record ' . $this->species . $this->common_name . ' is ' . $this->record_weight . '.';

In the second example you can use either double or single quotes.

Robbie Thomas
Robbie Thomas
31,093 Points

Tried that, I get this: Bummer! Did you add a getInfo method to the Trout class?

Strange.

I am sorry I was not clear. You need to fix the return in the getInfo() The getInfo would look like this:

<?php
function getInfo() {
  return $this->species . $this->common_name . ' tastes ' . $this->flavor . '.  The record ' . $this->species . $this->common_name . ' is ' . $this->record_weight . '.';
}

The rest of the code look ok to me. If you get a bummer please post the new code that you tried.

Robbie Thomas
Robbie Thomas
31,093 Points

Your code on: https://teamtreehouse.com/community/oop-php-challenge-6-of-7-concatenation-of-method worked out well.

Thanks for the help, I need to do some more research on PHP concatenation (spelled wrong likely). Plus, I should have made the subject 6/7 opposed to 7/7, my mistake there.

<?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) {
  parent::__construct($name, $flavor, $record);
     $this->species = $species;
  }   
  function getInfo() {
  return $this->species . $this->common_name . ' tastes ' . $this->flavor . '.  The record ' . $this->species . $this->common_name . ' is ' . $this->record_weight . '.';
}
  }

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

?>

This worked for me from the first challenge to challenge 7

Excellent. I formatted your code so it is readable.