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

Challenge 2 of 5

This is my code as per instructions, but I don't know what I'm doing wrong :(

Instructions say: "Define a constructor on the Fish class with 3 parameters, in order, named $name, $flavor, and $record." So I thought that's what I did?? I keep getting the ** "Bummer! Try again!" ** message.

Any thoughts are appreciated, thanks!

<?php

class Fish {

  // properties with default values
  public $name = 'This is the default name.';
  public $flavor = 'Chocolate :P';
  public $record = '200 lbs';

  // constructor method
  public __construct($name, $flavor, $record) {
    $this->name = $name;
    $this->flavor = $flavor;
    $this->record = $record;
  }

}

?>

Also tried this:

<?php

class Fish {
  public $common_name = 'Default name';
  public $flavor = 'chocolate';
  public $record_weight = 200;

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

?>

Still no luck :|

3 Answers

The code you posted in the comment is closer to the correct code. You don't have the correct public variable names in the code you posted first.

Try function __construct not public

Hello,

Your problem stems from the fact that the constructor is a class method and with all methods, they are PHP functions. You are not required to put PUBLIC before the constructor since PHP knows it's going to be public, however because it is a function, you need to declare it like you would a function.

Cheers!

D'oh! I see now, I seriously have to clean my glasses... Thank you guys!