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) Properties and Methods Mid-Course Challenge

Daniel Hildreth
Daniel Hildreth
16,170 Points

Need Help Challenge 5 of 5 Object Oriented PHP Basics

I do not understand what I am doing wrong here. Everything looks like it's in working order, but when I ran it, it said "Bummer! Try again!" Can someone help me figure out where I went wrong?

fish.php
<?php

class Fish {
  public $common_name = 'common_name';
  public $flavor = 'flavor';
  public $record_weight = 'record_weight';

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


public function getInfo(){
  return "A " . $this->common_name . "is an " . $this->flavor "flavored fish. The world record weight is " . $this->record_weight;
  }
}

$p = new Fish();
$bass = new Fish("Largemouth Bass", "Excellent", "22 pounds 5 ounces");

echo $p->getInfo();
?>
Daniel Hildreth
Daniel Hildreth
16,170 Points

BTW I forgot to post the instructions for this challenge. So here they are:

"Create a method on Fish named getInfo that takes no parameters and returns a string that includes the common_name, flavor, and record_weight for the fish. When called on $bass, getInfo might return "A Largemouth Bass is an Excellent flavored fish. The world record weight is 22 pounds 5 ounces.""

3 Answers

Daniel,

You are instantiating the class Fish twice when it should be one time. You are also calling the function getInfo using the object p (which you passed no parameters). Reread ALL instructions for all 5 challenges to see which ones stay and which ones need to be modified and removed. Hope this helps.

Cheers!

Daniel Hildreth
Daniel Hildreth
16,170 Points

Ok I now have another issue with challenge 1. I can't seem to find out what I'm doing wrong. Here are the instructions, "Add the following public properties to the Fish class: common_name, flavor, and record_weight." I have the following code:

<?php
class Fish {
  public $common_name = 'common_name';
  public $flavor = 'flavor';
  public $record_weight = 'record_weight';
}
?>

I get the error " Bummer! Error in precompile test." I have also tried this way too just like in the video:

<?php

class Fish 
{
  public $common_name = 'common_name';
  public $flavor = 'flavor';
  public $record_weight = 'record_weight';
}

?>

But I still get that error. Anyone know why?

Daniel,

The instructions ask for you to create the public properties. It looks like you did a little bit more than the instructions told you to do. Just do what the instructions state and the challenge should pass. HINT: remember, you are passing values in the constructor for the class to use. Hope this helps.

Cheers!