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

Rick Colella
Rick Colella
7,327 Points

PHP OOP constructor challenge

Clueless as to what's wrong with this code. I've tried to copy the videos syntax exactly (even though it doesn't fully cover the challenge - weird).

fish.php
<?php

class Fish {
  public $name = '';
  public $flavor = '';
  public $record = 0;  

  function__construct($name, $flavor, $record){
    $this->name = $name;
    $this->flavor = $flavor;
    $this->record = $record;
  }
}

?>

5 Answers

You lack a space between function and __construct. It should be:

<?php

function __construct($name, $flavor, $record) {
insert rest of code.
}

A dead giveaway that there is a problem on a line is the lack of coloration of the commands.

Your issue is following directions. It says to: "Add the following public properties to the Fish class: common_name, flavor, and record_weight." You added public properties named name, flavor, and record.

You have to remember that a computer is looking at this for a correct answer. It is often case sensitive also. There is a quiz where Javascript is wrong and JavaScript is correct.

This code passes the first stage of the challenge:

<?php

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

?>

Let us know if you have problems with the rest of the challenge.

Rick Colella
Rick Colella
7,327 Points

Actually no it isn't. I successfully passed the first stage of the challenge very easily by "following directions" as you put it. It's the second stage where it talks about parameters that is slipping me up. I changed the original properties from the first challenge back to $name, $flavor and $record in a desperate attempt to identify the issue. I know this it's almost definitely wrong to do, but I had no idea what else to do!

Thanks anyway, Ted.

Ok. You get responses based on the posted code.

Next, look at your spacing in the construct function after you fix your other names.

Rick Colella
Rick Colella
7,327 Points

Thanks for the response.

This is the code it won't accept (same as before but with the property names fixed). I'm basically back at square 1, scratching my head as I have followed the syntax in the video exactly, including spaces.

<?php

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

function__construct($name, $flavor, $record){ $this->name = $name; $this->flavour = $flavor; $this->record = $record; }

}

?>

Rick Colella
Rick Colella
7,327 Points

My God, that's it! Thanks Ted. I can't believe it.