What is $this useful for?

I was just reading about $this.

What would you / do you use it for?

As written it is just a variable. I am guessing that you mean in javascript routines.
In that case it does not have a question mark. And, in that case it means the currently selected object.
“this object” can be used to alter things such as attributes and other things…

1 Like

In PHP

$this is mainly used to refer properties of a class. It is the way to reference an instance of a class from within itself, the same as many other object oriented languages. From the PHP docs: The pseudo-variable $this is available when a method is called from within an object context.

I just like add besides using $this on methods (basically functions) it ($this) is used on properties (variables) as well in layman terms when inside a class.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service