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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsHow does the script know about the measurements array?
Hi all
I see that from the code we're able to create an array as a private property of the class.
But you also have the following code in which it seems to access the property so it knows which are correct measurements to use. How does it know about the array though?
It doesn't seem to have a settle that links the property to the rest of the code so it knows how to access it. Or did I miss it?
<?php
if($measure != null && !in_array(strtolower($measure), $this->measurements)) {
exit("<p>Please enter a valid measurement: " . implode(", ", $this->measurements) . "</p>");
}
?>
Thanks :)
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Jonathan,
Declaring it private only means that it can't be accessed outside the class. Methods of the Recipe class still have access to private properties.
The code you posted is from the addIngredient method which is part of that class. So it has direct access to the private properties.
What's happening with $this->measurements
is the same as what's happening with the title property that was also declared private.
The getTitle and setTitle methods are able to access the title property with $this->title
Is that what you were asking about?
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsSorry, yes, my post wasn't as clear as I thought. :p
What I meant was there was no "setter" function for the $measurements like the $title property. But my understanding was that's what we use "getter" and "setter" functions for.
But I think I'm getting it now. The code I posted kind of acts as it's own "setter" and it's able to access the property in the same way because it's still inside of the class?
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsYes, methods inside the class have direct access to private properties. Getters and setters are not needed for this.
The getter and setter for the $title property was only needed to provide access to the title from outside the class.
Notice here that Alena was able to set the source property directly, because it was public, but had to use the setter for the title since it was private.
This is for code outside the class but for methods inside the class they would have direct access to both the source and the title property using
$this->source
and$this->title
respectively. It doesn't matter within the class that one is private and the other is public.Alena didn't create a getter and setter for the $measurements array probably because it's meant for internal use only. She didn't want outside code having access to this array and being able to modify it.