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 Building the Recipe Static Methods

Why 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


Beacuse it's a new another (static) class! :)

Elaborating 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.

I 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;

To sum it up, the argument we pass in when we're calling the method will influence the result.