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

5/7: How do I add values to object?

How do I add values to the object $brook_trout? I made it an object of class Trout, so it should've inherited the variables there. I made a function (getInfo) that returns the variables, which $brook_trout calls, and I inserted the values there.

class Trout extends Fish {

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

function getInfo() { return $this -> name, $this -> flavor, $this -> record, $this -> species; } }

public $species;

$brook_trout = new Trout(); $brook_trout -> getInfo("Trout", "Delicious", "14 pounds 8 ounces", "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 {

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

  function getInfo() {
    return $this -> name, $this -> flavor, $this -> record, $this -> species;
  }
}

  public $species;

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




?>

2 Answers

Hi Stefan,

Your $species property should be inside your Trout class, not after it.

Also, the getInfo() method doesn't take any arguments. It's the constructor that takes all those arguments.

You want to pass in all those arguments when you create the object in this code: $brook_trout = new Trout();

Thanks for your help, Jason. I managed to complete the challenge.