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 PointsWhy did $recipe replaced $this?
Sorry but i am totally lost here, In the video i am lost on what is going on. Alena passed $recipe as a argument and then replaced $this with $recipe, why? I don't understand, what is the use of this?
3 Answers
Szabolcs Szána
6,961 PointsHi,
Beacuse it's a new another (static) class! :)
Peter Furler
31,647 PointsElaborating on Szabolcs answer, $recipe is the parameter we used to pass an argument to within our method, which in this case our argument is $recipe1.
The pseudo variable $this is only used to refer to any future instantiated (single occurrence) object from inside the class that the object is created from. Which in our case is $recipe1 which is an object belonging to the Recipe class.
jlampstack
23,932 PointsI was having problems with this too, until a lightbulb finally went on in my head. Will do my best to explain my understanding below.
The entire method / function is....
public static function displayRecipe($recipe)
{
return $recipe->title . " by " . $recipe->source;
}
The section of the code we're modifying each time is....
return $recipe->title . " by " . $recipe->source;
Which is equivalent to....
return $recipe1->title . " by " . $recipe1->source;
return $recipe2->title . " by " . $recipe2->source;
return $recipe3->title . " by " . $recipe3->source;
etc.
To sum it up, the argument we pass in when we're calling the method will influence the result.