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

Luke Briedis
Luke Briedis
3,600 Points

PHP OO Basics 6/7 final challenge not passing

Not sure why this isn't passing. It's 1:1 with the examples used in the videos. I even tried using the echoing what the proposed example is, though it shouldn't require that exactly right ?

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."

<?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->name . "tastes" . $this->flavor . ". " . "The record" . $this->species . $this->name . "weighed" . $this->record . ".";
  }

}

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


?>

10 Answers

hint for ya: public $common_name; is the actual property we want.

It's the actual property that we want where, exactly? This doesn't make sense to me in the context of this problem step.

[UPDATE] Ah, I figured it out. Later in the output string, we don't explicitly output the name and species, we just return the "common_name". I think what confuses many of us is that's not at all obvious in the return string given in the instructions, which say "The record Brook Trout weighed..." so, it looks like we simply insert $this->species and $this->name again for "Brook Trout" in that string.

Brian Molkovich
Brian Molkovich
11,333 Points

I got right answer, if somebody needs)))

I do. The "Bummer. Try again!" message seems to be the only "guidance" offered here. So frustrating.

Jorge Talancon
Jorge Talancon
5,416 Points

Stewart. I use sublime text on localhost to check out my errors. Because you do get a lot more information about your mistakes. Hope this tip works for you!

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

}

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

  }

?>
Luke Briedis
Luke Briedis
3,600 Points

Ic. Then I also have to use $record_weight instead of $weight. This is confusing, because in the earlier lesson, $weight becomes the variable for $record_weight, but upon reflection it was not extended this far in the lesson. One would just assume it would be addressed the same, I guess I know better now.

Grant LeMahieu
Grant LeMahieu
11,301 Points

I used the syntax in the parent class getInfo. Hint: name and species together

I can't pass this. My code:

public function getInfo() { $output = "The {$this->species} 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;

Any thoughts? Thanks.

Nevermind, was missing a bracket.

Steve Berrill
Steve Berrill
20,016 Points

Heres what I did

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

I actually struggled because I forgot to close the function!

Wouter Delken
Wouter Delken
3,553 Points

Can't believe it, I also forgot to close the function! Caught it now, so thanks for your comment :-)

Also can't believe it... I tried many times and finally found forget to close it....

Jeremiah Wodke
Jeremiah Wodke
3,192 Points

I know I'm way off but I don't understand why. Can somebody help me pass this brutal Quiz? lol

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

  function __construct{

    $this->Trout = $common_name;
    $this->Delicious = $flavor;
    $this->14_pounds_8_ounces = $record_weight;
    $this->Brook = $species;

  };

  public function getInfo($common_name, $flavor, $record_weight, $species) {
    $output  = "The $common_name tastes $flavor. The record $species $common_name weighed $record_weight.";
    return $output;
    }

};

The names of the arguments that you use in the construct are irrelevant, only the property name matters. However, you do need to name them with some level of readability.

for an example (this works but is not really readable to someone who did not write it):

public function __construct($arg1, $arg2, $arg3, $arg4) {
    parent::__construct($arg1, $arg2, $arg3);
    $this->species = $arg4;
}
Juan Jimenez
Juan Jimenez
11,245 Points

I can't get it to pass following this example

class Trout extends Fish {

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