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 trialBecky Castle
15,294 PointsWhat is the relationship between an OOP property and a construct?
What are the relationships between the properties and construct[or]s? Why can I define "$name, $flavor" and "$record" without ever mentioning "$common_name" or "$record_weight"?
Thanks ;-)
<?php
class Fish {
public $common_name;
public $flavor;
public $record_weight;
function __construct($name, $flavor, $record){
$this-> name = $name;
$this-> flavor = $flavor;
$this-> record = $record;
}
}
?>
3 Answers
Hugo Paz
15,622 PointsHi Becky,
You cant define them like that, you need to define them with the properties of the class.
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;
}
}
Becky Castle
15,294 PointsHugo, thanks for your help!
I'm still confused about constructors; in the challenge, we assigned those new names to the properties, but then when it came time to echo it to the screen, we still used the old variable name. Like this:
return "A " . $this->common_name . " has " . $this->flavor . " flavor. Its record weight is " . $this->record_weight "."
We didn't even use the " $name ", " $flavor ", and " $record ". So why would we rename them?
Thanks again!
Hugo Paz
15,622 PointsHey Becky,
We don't rename them, those are arguments you use in the constructor function.
When you create an object of the type fish (its class), that fish object has 3 properties which need to be set when you create it.
if the constructor was something like this:
function __construct($a, $b, $c){
$this->common_name = $a;
$this->flavor = $b;
$this->record_weight = $c;
}
the end result would be the same. Those variable names are used within the function itself. You use their values to set the object properties.
So let us create a fish object:
$our_fish = new Fish("Tuna", "Great", "20 pounds");
Our fish has 3 properties, $common_name, $flavour and $record_weight.
The constructor will set them for us with the values we supplied when we created a new Fish.
So before the constructor does anything, our fish properties look something like this:
$common_name = ?
$flavour = ?
$record_weight = ?
Then constructor takes the values we inserted when we created the fish and applies them to the properties
function __construct($a ("Tuna"), $b ("Great"), $c (20 pounds)){
$this->common_name = $a;
$this->flavor = $b;
$this->record_weight = $c;
}
so now our fish properties look like this:
$common_name = "Tuna"
$flavour = "Great"
$record_weight = "20 pounds"
Becky Castle
15,294 PointsThanks for the explanation, Hugo! I needed to be reminded that those are arguments. I think I'm getting a better grasp on this: construct functions and their arguments are like a buffer for the class' properties -- kind of like a spare house key? (You could just have the one key, but it's better to use a spare and save the master key so nothing unfortunate happens to it. Maybe?) (Lol! I'm sorry for buggin' you! This OOP stuff is sooo abstract to me; my brain needs something tangible to liken it to. But maybe you should just get a delicious cup of coffee, and pretend not to notice anymore of my posts, hee hee hee.)