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 task 3 of 5

This is a question for:

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

I have played around with it, and the error it is giving me is:

Bummer! Be sure the $name parameter is assigned to the common_name property.

I have public $common_name = 'name'; with __construct( $name, and $this->name = $common_name; .

I do not know if I have something backwards or not... but the task will not validate.

class Fish {

  public $common_name = 'name';
  public $flavor = 'flavor';
  public $record_weight = 0;

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

  public function getInfo(){
    return "Fish:". $this->name;  
  }

}

$haddock = new Fish('Haddock','Plain','6');

Still nothing and I even shortened it down to just:

class Fish {

  public $common_name = '';
  public $flavor = '';
  public $record_weight = '';

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

}

5 Answers

You need to change the places of $name and $common_name in the construct method. You're supposed to be assigning the argument $name to $this->common_name and not the other way around, so it should be $this->common_name = $name.

Thank You, I did same thing

David Endersby
David Endersby
24,915 Points

Finally figured it out

<?php

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

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

?>```

^_^ OMG thank you I knew I had to be mixing something up.

were did i go wrong class Fish {

public $common_name ; public $flavor ; public $record_weight ;

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

}

You need to add $common_name = something. Same for the other objects.

amadeo brands
amadeo brands
15,375 Points

Hard one.

I got it working with this code:

<?php 

class Fish {

  public $common_name;
  public $flavor;
  public $record_weight;

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




?>