$this and self
This lesson discusses the use of this and self in accessing members in classes.
We'll cover the following
$this
The pseudo-variable $this
refers to a member of a class according to an instance of that class. It provides a reference to the calling object, that is the object to which the method or property belongs to. Note that the member of the class must be non-static.
The Object Operator
The ->
symbol is a built-in construct in PHP that is used with the $this
keyword to access contained methods and properties.
self
In PHP, like many other languages, the self
keyword refers to properties and methods inside the scope of a class. This pseudo-variable provides a reference to the calling object, that is the object to which the method or property belongs to.
The Scope Resolution Operator
The ::
symbol, known as the scope resolution operator is an in-built construct in PHP that is used to access contained methods and properties. It is used with the self
keyword.
this versus self
- Use
$this->member
for accessing non-static members (methods and properties).- Use
self::$member
for accessing static members (methods and properties).
Example
Run the code below to see how you can use $this
and self
in PHP.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy