Link to home
Start Free TrialLog in
Avatar of LB1234
LB1234

asked on

Having trouble understanding the concept of $this. I've searched for several explanations, and none clarify the concept for me.

Having trouble understanding the concept of $this. I've searched for several explanations, and none clarify the concept for me.

I don't understand its purpose and why its used.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

In a class definition, there are some variables that should be encapsulated strictly inside one method of the class, but there are other variables that should be shared between methods of the class. These sharable variables are called "properties."  To create one of these sharable properties, you use the pseudo-variable $this-> and append a name to it.
http://php.net/manual/en/language.oop5.basic.php
http://php.net/manual/en/language.oop5.visibility.php
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LB1234
LB1234

ASKER

Thanks Ray.  What thing or entity does $this represent?  Since it is a variable it must stand for some other thing.  What is that thing?  What could I use in place of $this, if anything?
It's more of a language construct than a "thing."  Just like we use the word "Class" to define a class in PHP, we use the word "$this->" as a prefix to a variable name when we want that variable to become a property of the object.
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LB1234

ASKER

"as a prefix to a variable name when we want that variable to become a property of the object."

In the sentence above, which object are you referring to?
Avatar of LB1234

ASKER

Dave, unless the concept is already pretty straightforward, I find it's very rare that php.net is a source of clarification.  I looked at their explanation many times before posting my question at EE.  Whoever pens that stuff is clearly a great programmer, but an awful communicator, especially considering that the site, i'm guessing, is intended to help clarify concepts for new programmers PHP as well as being a handy reference for those who've largely mastered it.

There's something to be said for being economical in your wording, but I find PHP.net to actually be terse.
I understand... but it's still better than most sites for languages.  I'm not sure there is anything 'straightforward' about OOP.
Avatar of LB1234

ASKER

Ray, ok, I'm sort of putting together my understanding of this piece by piece.  Let me see if I have this right so far. $this is only used with methods, and it's purpose is not itself to be a variable but to identify a variable in the class as a property of that class?  

But aren't the variables in question ALREADY properties of that class by having been placed within the classes curly braces?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LB1234

ASKER

Ray, thanks a million for taking the time write all of that out.  I appreciate it, and finally I understand it now.  I guarantee this is the most indepth explanation on the internet that's easily available, because I googled for a few hours before finally posting to EE.  It's really amazing how much other descriptions have left out!!
Thanks for your kind words.

Now here is an important follow-on message.  You will make this mistake; you cannot avoid it.  We all make this mistake.

You will forget to use $this-> at some point in your life.  When you do, an object property will go missing, and instead you will just have a local variable.  And things will not work well, usually resulting in a run-time error.

Now that you are aware that you will make this mistake, you can be on the lookout for it!

Best regards, ~Ray
Avatar of LB1234

ASKER

Thanks again, Ray, so let me expand on my previous understanding of $this (while hopefully not going off the rails regarding my current understanding).  To mix concepts for a moment -- $This is basically a form of automatically passing arguments to the method being called?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Footnote: I just tested and $this-> is case-sensitive.
Avatar of LB1234

ASKER

Howdy Ray, I'm really starting to get this!

I tried the following code, but got unexpected results. I was expecting 6 and 8 to be returned but I got 2 and 4 with an error message saying, Notice: Use of undefined constant wheels - assumed 'wheels':

<?php

class Car {
	
	public $doors;
	public $wheels;
	
	public function add() {
		echo $this->doors + wheels;
			
	}

	
}

$sports_car = new Car;
$hatchback = new Car;

$sports_car->doors = 2;
$sports_car->wheels = 4;

$hatchback->doors = 4;
$hatchback->wheels = 4;

$sports_car->add();
$hatchback->add();
?>

Open in new window

Avatar of LB1234

ASKER

Oh oops, I forgot to preface "wheels" with $this->

Ok Ray I finally got the concept.  Really, thanks so much for the example code and links and explanations.  I appreciate it incredibly.  Really.

Have a good one.
Oh oops, I forgot to preface "wheels" with $this->

Ha!  Yes, we all do that.  You will do it a few more times, too, if you're anything like me.  But at least now you've seen the symptom and found the issue fairly quickly.

Thanks for the points and thanks for using E-E, ~Ray
Avatar of LB1234

ASKER

Lol, it didn't take me very long to do the very thing you'd said we'd all sooner or later forget to do.  I guess it much sooner rather than later. :)
Yes!  I am a great believer in making lots of mistakes fast.  It's a great way to make progress, just like Edison and lightbulbs.
Avatar of LB1234

ASKER

Ray, I guess I do have more one more question about $this, and it's probably what's throwing a lot of people off about it.  Are you still seeing these comments?