Differences between self- and this-PHP
Contents
Differences between self and this in PHP
In the PHP programming language, `self` and `$this` are keywords used within class definitions to refer to members (properties and methods), but they serve different purposes related to the distinction between static and instance contexts.[1][2] The `self` keyword refers to the current class itself, while `$this` refers to the current object instance of the class.[3] This fundamental difference dictates when and how each keyword is used.
The primary distinction lies in their relation to static and non-static class members.[1] `self` is used to access static members, which belong to the class as a whole rather than to any specific object created from it.[4] To access these, `self` is paired with the scope resolution operator (`::`).[1][5] Conversely, `$this` is used to access non-static (or instance) members, which are specific to each object.[2][4] It is a pseudo-variable that represents the specific instance and is used with the object operator (`->`). Because static members are not tied to an instance, the `$this` pseudo-variable is unavailable in methods declared as static.
Comparison Table
| Category | self | $this |
|---|---|---|
| Reference | Refers to the current class definition.[2][3] | Refers to the current object instance.[3] |
| Context | Used in a static context to access static properties and methods.[1][4] | Used in a non-static (object) context to access instance properties and methods.[1] |
| Operator | Scope Resolution Operator (`::`). | Object Operator (`->`).[1] |
| Usage Syntax | `self::$staticProperty;` or `self::staticMethod();` | `$this->property;` or `$this->method();` |
| Instantiation Requirement | Does not require an object instance to be created. | Requires an object of the class to be instantiated. |
| Inheritance Behavior | In the context of inheritance, `self` always refers to the class where the method was originally defined. | Refers to the specific instance of the class that is calling the method, which can be an instance of a subclass.[3] |
Static Context vs. Instance Context
In object-oriented programming, a class can have two types of members: static and instance (non-static). Static members are associated with the class itself and are shared among all instances of that class. They can be accessed directly from the class name without creating an object. The `self` keyword provides a way to refer to these members from within the class definition.
Instance members, on the other hand, belong to a specific object created from the class. Each object has its own set of instance properties, and their values are independent of other objects of the same class. The `$this` keyword is used within a class's methods to work with the instance members of the specific object that the method was called on.
Late Static Binding
While `self` refers to the class in which it is written, PHP also provides the `static` keyword for late static bindings. Introduced in PHP 5.3, late static binding allows a reference to be resolved in the context of the class that is called at runtime, rather than the class where the method is defined. When used in the context of inheritance, `static::` will refer to the subclass that is being used, whereas `self::` will continue to refer to the class where the method was originally defined. This provides more flexibility when designing extensible class hierarchies.
References
- ↑ 1.0 1.1 1.2 1.3 1.4 1.5 "phppot.com". Retrieved February 06, 2026.
- ↑ 2.0 2.1 2.2 "geeksforgeeks.org". Retrieved February 06, 2026.
- ↑ 3.0 3.1 3.2 3.3 "stackoverflow.com". Retrieved February 06, 2026.
- ↑ 4.0 4.1 4.2 "dev.to". Retrieved February 06, 2026.
- ↑ "dinocajic.com". Retrieved February 06, 2026.
