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

Don Shipley
Don Shipley
19,488 Points

PHP OO Basics 6/7 final challenge

Add a getInfo method to your Trout class that returns a string containing the common_name, flavor, record_weight, and species properties. For example, on our brook trout it might return "Brook Trout tastes Delicious. The record Brook Trout weighed 14 pounds 8 ounces.

class Trout extends Fish { public $species; function __construct($name, $flavor, $record, $species){ parent::__construct($name, $flavor, $record); $this->species = $species; }

public function getInfo() { return "Brook" . $this->common_name . "Tastes" . $this->flavor . " The record" $this->record_weight . $this->species ); } } $brook_trout = new Trout("Trout" , "Delicious" , "14 pounds 8 ounces" , "Brook");

4 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Don;

If you follow the Markdown Cheatsheet it helps tremendously in attempting to see the code in which you are working. That being said, the following code got me through task 6:

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


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

   public function getInfo() {
    return $this->species . $this->common_name . "tastes" . $this->flavor . ". " . "The record" . $this->species . $this->name . "weighed" . $this->record_weight . ".";
  }

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

?>
Don Shipley
Don Shipley
19,488 Points

Thank you for your reply. I forget the echo $brook_trout->getInfo(); and had to move the curly brace to end the function correctly.

`class Trout extends Fish { public $species; function construct($name, $flavor, $record, $species){ parent::construct($name, $flavor, $record);

$this->species = $species; } public function getInfo() { return ( "Brook " . $this->common_name . " Taste " . $this->flavor . " The record Brook Trout weighed " . $this->species . $this->record_weight); } } $brook_trout = new Trout("Trout" , "Delicious" , "14 pounds 8 ounces" , "Brook");

echo $brook_trout->getInfo();`

Łukasz Czuliński
Łukasz Czuliński
8,646 Points

Looks like you have some syntax errors.

As for the code not passing, I think it might be that there is an extra parentheses ) after $this->species in your getInfo() function that shouldn't be there.

Also, when concatenating properties, don't forget to add some space in between. For example, you have written: $this->record_weight . $this->species

It should be: $this->record_weight . " " . $this->species

EDIT: looks like you got a response already.

Don Shipley
Don Shipley
19,488 Points

No I added the space in the string . " string " . Thank you for your reply..

TASK 3

return $output; } }

class Trout extends Fish { public $species;

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

}

?>