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

Brandon Wiemer
Brandon Wiemer
2,705 Points

Stuck

I know what a constructor is and what the properties are however I don’t understand what it the corresponding parameter variable is? What do I need to do?

2 Answers

It's just asking you to assign the values passed to the constructor to the public properties you defined earlier, so for example if you had defined a public property like this: public $fav_color, and your constructor receives a parameter $color you'd do something like this: $this->fav_color = $color; inside the __construct function.

Hello,

If I understand your question, you are asking why there are parameter values in the constructor? Hopefully I'm right and if not maybe my explanation will answer your real question. A constructor is automatically called when a class is instantiated. If you do not have a constructor declared, PHP will bypass it and just create the class (kind of like how destructors are). I know you haven't learned what a destructor is but, in short, it's a function that's (if it's been declared) will be called when the class gets destroyed or the script in which the class has been created in is terminated. It's there for clean up basically. Sometimes you want to allow a script instantiating a class to pass information to that class for whatever reason. This allows you to initially set values inside of a class when you create the class without really doing much else. In the videos and the code challenges, you are creating a constructor with function arguments that adds information to the class when the class is created. In the Fish class it's 3 parameters and in the Trout class it's 4. In the Fish class you have three properties that are declared without values. When you create the class, you are basically populating those properties because of the construct function. You are sending the properties a value when you create the class by adding information to the parameters of the construct function. It's the same thing in the Trout class as, since you added an additional property, you are now passing 4 pieces of information instead of 3 to populate the properties.

You are not required to have parameters in the construct; however, if you want to perform a task or add information to the class when you create it and before you begin to use the class, adding parameters to the construct is how you would pass in the data. Hope I've been able to answer your question.

Cheers!