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 trialammarkhan
Front End Web Development Techdegree Student 21,661 Pointsobject and assigning values.
Not sure, if i am doing it right. Do i need to use $this? or am i doing something else wrong?
<?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;
}
}
$bass = new Fish();
$bass->$name = "Largemouth Bass";
$bass->$flavor = "Excellent";
$bass->$record = "22 pounds 5 ounces";
?>
2 Answers
Pedro Garcia
8,426 PointsHi again Ammar, As you can notice you already have a construct function, so this function will help you to add the values to the Class instance, in this case a new Fish. So your code should look like this:
$bass = new Fish("Largemouth Bass", "Excellent", "22 pounds 5 ounces");
The new Object takes the 3 parameter values, then the constructor runs and add each value to the the Object, you don't need to add them one by one as you tried, notice that the order is important.
Hope i was clear.
Jason Anello
Courses Plus Student 94,610 PointsHi Ammar,
The constructor accepts 3 arguments: name, flavor, and record. All you need to do then is pass the 3 values in when you create the object.
$bass = new Fish("Largemouth Bass", "Excellent", "22 pounds 5 ounces");
ammarkhan
Front End Web Development Techdegree Student 21,661 Pointsammarkhan
Front End Web Development Techdegree Student 21,661 PointsIf i add them one by one, will there be a problem. although i am just asking for my information sake.