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 Building the Recipe Controlling the Class

Johann Wichtig
Johann Wichtig
560 Points

PHP object-oriented, tast 2 of 3,

The task is "In the new constructor method, assign each of the properties on the Fish class with its corresponding parameter variable."

Keep getting this:

"Bummer! Be sure the $name parameter is assigned to the common_name property. Use $this to refer to the current object."

Been at this for so long and can't seem to crack it. Any help would be appreciated.

fish.php
<?php

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

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

?>

1 Answer

Joel Bardsley
Joel Bardsley
31,246 Points

You're close, you've just got your $this in the wrong place! As $common_name, $flavor and $record_weight are properties of the Fish class, you need to set $this->property_name equal to the values passed to the constructor, i.e.:

<?php
class Person {
  // Declare properties
  public $person_name;
  public $hair_color;
  public $is_allergic_to_nuts;

  public function __construct($name, $hair, $nuts) {
    $this->person_name = $name;
    $this->hair_color = $hair;
    $this->is_allergic_to_nuts = $nuts;
  }
}

// Create new Person
$jim = new Person("Jim", "Brown", true);

For further clarification, if you refer to the getInfo function in the code challenge itself, you'll see how the properties are being used/returned.

Hopefully that helps, and good luck.