Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,242 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.