Bloggers unite

"Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid." – Albert Einstein


Difference between PHP self and this


Let’s understand a few differences between self and this:

self

this

self keyword is not preceded by any symbol.

this keyword should be preceded with a $ symbol.

To access class variables and methods using the self keyword, we use the scope resolution operator ::

In case of this operator, we use the -< symbol.

It is used to refer the static members of the class.

It is used to access non-static members of the class.

PHP self refers to the class members, but not for any particular object. This is because the static members(variables or functions) are class members shared by all the objecxts of the class.

Whereas, $this wil refer the member variables and function for a particular instance.



Leave a comment