I see the variable $this
in PHP all the time and I have no idea what it's used for. I've never personally used it, and the search engines ignore the $
and I end up with a search for the word "this".
Can someone tell me how the variable $this works in PHP?
Lets see what happens if we won't use $this and try to have instance variables and constructor arguments with the same name with the following code snippet
It echos nothing but
this echoes 'Tom'
It's a reference to the current object, it's most commonly used in object oriented code.
Example:
This stores the 'Jack' string as a property of the object created.
$this
is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).