Difference Between self and $this in PHP OOP
Understanding the distinction between $this and
self is fundamental to mastering object-oriented
programming (OOP) in PHP. While both keywords are used to reference
members within a class, they serve entirely different purposes based on
context: $this refers to the current object instance,
whereas self refers to the class itself. This article
provides a direct, clear comparison of these two keywords, explaining
when and how to use each with practical code examples.
Understanding $this
The $this keyword is a reference to the current object
instance. It is used to access non-static properties and methods
belonging to a specific object that has been instantiated from the
class.
Because $this relies on an active object instance, you
cannot use it inside static methods, as static methods can be called
without creating an object. When using $this, you must use
the object operator (->).
Example of $this:
class User {
public $name;
public function __construct($name) {
// $this refers to the specific object being created
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
$user = new User("Alice");
echo $user->getName(); // Outputs: AliceUnderstanding self
The self keyword refers to the class itself, rather than
a specific instance of that class. It is used to access static
properties, static methods, and class constants.
Since static members belong to the class blueprint rather than any
individual object, self is used in conjunction with the
scope resolution operator (::). It can be used in both
static and non-static methods to access static members.
Example of self:
class Counter {
public static $count = 0;
const LIMIT = 100;
public function increment() {
// self refers to the class-level static property and constant
self::$count++;
}
public static function getLimit() {
return self::LIMIT;
}
}
$counter = new Counter();
$counter->increment();
echo Counter::$count; // Outputs: 1
echo Counter::getLimit(); // Outputs: 100Key Differences At a Glance
| Feature | $this |
self |
|---|---|---|
| Reference | Refers to the current object instance. | Refers to the current class. |
| Context | Non-static context (instance methods). | Static context (but can also be used in non-static methods). |
| Operator Used | Object operator (->) |
Scope resolution operator
(::) |
| Accesses | Non-static properties and methods. | Static properties, static methods, and constants. |
| Syntax | $this->propertyName (No
$ on the property name) |
self::$propertyName (Requires
$ on the property name) |
Combining $this and self in a Single Class
A single class can utilize both keywords depending on whether the member being accessed is static or instance-specific.
class Product {
private $price; // Instance property
private static $taxRate = 0.1; // Static property
public function __construct($price) {
$this->price = $price; // Accessing instance property
}
public function getTotalPrice() {
// Accessing instance property and static property together
return $this->price + ($this->price * self::$taxRate);
}
}
$product = new Product(100);
echo $product->getTotalPrice(); // Outputs: 110By keeping this distinction in mind—$this for
instance-specific data and self for class-wide static
data—you can write clean, bug-free object-oriented PHP code.