When should I use 'self' over '$this'?

When should I use 'self' over '$this'?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

When should I use 'self' over '$this'?

In PHP, the distinction between $this and self is subtle but significant, particularly when dealing with inheritance hierarchies. Both keywords allow you to reference the current class or instance, yet they resolve differently at runtime. Understanding this difference prevents subtle bugs where a parent class property is unexpectedly shadowed by a child class definition, or vice versa.

The most common scenario arises in PHP 5 and later, where late static binding plays a crucial role. When you use $this, you are referring specifically to the instance of the object being called. This reference is bound to the specific runtime instance. If you are inside a child class method and access a property via $this, PHP looks for that property on the current instance's class. It does not automatically look up the inheritance chain unless you explicitly use magic methods like __get or properties defined in parent classes without shadowing.

Consider a scenario where you have a base class User and a child class Admin. If the User class defines a static property $role, accessing it via $this::role inside a method will return the value defined in the User class, regardless of whether the current object is an instance of User or Admin. However, if you use self::role, PHP resolves the name to the class where the code was written. If the Admin class overrides that property, self will pick up the override, whereas $this might still point to the parent's definition depending on how the binding occurs.

class User {
    public static $role = 'guest';

    public function getRole(): string {
        return self::$role; // Resolves to User::$role
    }
}

class Admin extends User {
    public static $role = 'admin';
}

// Output: guest
echo (new Admin())->getRole(); 

In the example above, self resolves strictly to the class where the method is defined (User). This behavior can lead to surprising results if you expect child classes to have their own static properties automatically reflected in parent methods. Conversely, $this refers to the instance. While less common for accessing static properties directly (since you'd typically use the class name or self), $this is essential when dealing with dynamic property access or when you need to ensure you are operating within the context of the specific object's state.

The key trade-off lies in late static binding. If you intend for child classes to have their own distinct static properties that should be accessible from parent methods, using self inside the parent class will break that expectation because it hardcodes the lookup to the parent class name. In such cases, relying on the class name directly or using static is often preferred over self.

Using $this is generally appropriate when you are manipulating instance data or when the specific runtime type of the object matters for polymorphic behavior. It ensures that the logic remains tied to the object's identity rather than its static class definition. However, mixing $this with static property access inside a method can be confusing and often indicates a design pattern issue where static state is being accessed via an instance variable.

Ultimately, the choice depends on whether you want the reference to be resolved at compile time against the current class definition (self) or if you need the reference to reflect the actual runtime class of the object (static or direct class name). Avoid using self when you anticipate that child classes will override static properties and need those overrides to be visible from parent methods. Instead, use static for late static binding or explicit class names for clarity.

Our network