differences between "self", "$this", "::" and "->" and when / how to use them
I am Struggling to understand the differences between "self", "$this", "::" and "->" and when / how to use them in classes. Any help would be appreciated, also if you could dumb it down to plain english that would help alot! Thanks in advance
(In what I've been reading it's been saying stuff about static methods stuff and I'm not sure when / how this influences it - am I going down the wrong path?)
"self" (and "parent") essentially refer to a class in general, while "$this" refers to the specific instance of the class, i.e. "this object". The "->" operator is used with objects, so will be what follows "$this", while the "::" operator is used with the more general class reference, so will be used with "self" (or "parent").
"self" is used in a static context, either within a static method, or to reference a static class variable anywhere within a class, while "$this" refers only to the current instance (object), so would be used in any non-static method (which generally is the more common case).
The "::" operator is similarly used to call a static method or parameter from outside of the class, e.g. "ClassName::methodName()".
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
How to Ask Questions the Smart Way
(not affiliated with this site, but well worth reading)
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)