In PHP 5, what is the difference between using self
and $this
?
When is each appropriate?
self
(not $self) refers to the type of class, where as $this
refers to the current instance of the class. self
is for use in static member functions to allow you to access static member variables. $this
is used in non-static member functions, and is a reference to the instance of the class on which the member function was called.
Because this
is an object, you use it like: $this->member
Because self
is not an object, it's basically a type that automatically refers to the current class, you use it like: self::member