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 Methods

Easy way to remember $this

I was wondering if there is any metaphor that can be used to remember this, like a example?

1 Answer

Daniel Dou
Daniel Dou
2,014 Points

I remember it like this. So, you are basically referring to the properties of 'this' object. Not any other object, but this object that you're currently working on.

So I think this is the way it goes... for example, let's say you have a class called Fruit.

So you create a new instance of that class, which is an object called Apple.

The newly created object called $apple basically belongs to the Fruit class (or category).

When you create the object called Apple, since it's a Fruit, it should have all the properties of a fruit. Or else you couldn't call it a fruit.

Maybe one of the properties is of the Apple that it gets as a fruit is $seed. Since one of the requirements to be a fruit is that you need to have seeds.

So the newly created Apple object comes along with the $seed property.

The $seed property tells you how many seeds are in the fruit.

Every fruit has a different number of seeds.

So let's say the class also contains a function that echos the number of seeds available. And that passes over to the object (in this case $apple), which means you can echo the number of seeds in $apple too. Along with any other new Fruit created.

So you already know that, every time you create a new object (like $apple), it receives all the functions and properties of the main class (in this case, Fruit).

So if the Fruit class contains $seed property, and number_of_seeds() method. Anything created as part of the Fruit class will also get the $seed property and number_of_seeds() method too.

In this example, the new $apple object (created from the Fruit class) gets the $seed property and number_of_seeds() method as part of being in the Fruit class.

However, when it receives (or I think it's called inherits) these properties and methods, there needs to be a way to refer to them.

So for example, let's say we had this original method/function within the class:

public function number_of_seeds() { return "Number Of Seeds: ". ???? ->seed; }

What should you replace the question marks (????) with, so that every time a newly created object receives this function as part of being of the class, it makes it easy to pass down?

If you did this:

public function number_of_seeds() { return "Number Of Seeds: ". fruit->seed; }

Then it wouldn't make sense. Because if I call:

echo $apple->number_of_seeds();

I don't want to know the number of seeds in simply any fruit in general (fruit->seed).

I want specifically to know the number of seeds in THIS $apple. And only this $apple.

That's why it makes sense to replace the ???? with "this":

public function number_of_seeds() { return "Number Of Seeds: ". this ->seed; }

If I created an object called $peach of the Fruit class, then it would also inherit/get this method as a member of the Fruit class:

public function number_of_seeds() { return "Number Of Seeds: ". this ->seed; }

And so, using this method/function, I could automatically call and get the number of seeds in THIS peach. Without any additional references.

Sorry if I made it even more confusing for you. Ha ha.

Hopefully not.

Casey Cross
Casey Cross
9,289 Points

Great answer Daniel, that really helps all this make more sense.

Jen C
Jen C
8,695 Points

This is really helpful Daniel, thank you!