In PHP, $this is a special variable used inside a class to refer to the current instance of the class. It is used to access the properties and methods of the current object.

Key Uses of $this

  1. Accessing Properties

    You can use $this to access the properties of the current object.

      <?php
    class Car {
        public $make;
        public $model;
    
        public function __construct($make, $model) {
            $this->make = $make;
            $this->model = $model;
        }
    
        public function displayInfo() {
            echo "Make: " . $this->make . ", Model: " . $this->model;
        }
    }
    
    $car = new Car("Toyota", "Corolla");
    $car->displayInfo(); // Outputs: Make: Toyota, Model: Corolla
    ?>
      
  2. Calling Methods

    You can use $this to call other methods within the same class.

      <?php
    class Calculator {
        public function add($a, $b) {
            return $this->calculateSum($a, $b);
        }
    
        private function calculateSum($a, $b) {
            return $a + $b;
        }
    }
    
    $calc = new Calculator();
    echo $calc->add(5, 10); // Outputs: 15
    ?>
      
  3. Chaining Methods

    You can use $this to return the current instance from a method, allowing for method chaining.

      <?php
    class Person {
        private $name;
    
        public function setName($name) {
            $this->name = $name;
            return $this; // Return current instance
        }
    
        public function getName() {
            return $this->name;
        }
    }
    
    $person = new Person();
    echo $person->setName("Alice")->getName(); // Outputs: Alice
    ?>
      
  4. Accessing the Current Instance

In object-oriented programming, $this is essential for interacting with the current object instance, especially within methods, constructors, and when accessing or modifying instance properties and methods.

Key Points

  • Instance Reference: $this refers to the current instance of the class.
  • Property and Method Access: Use $this to access or modify properties and call methods within the same object.
  • Method Chaining: Return $this from methods to enable chaining of method calls.

$this vs self vs static

Keyword Refers to
$this Current object instance
self:: Class where code is written
static:: Class resolved at runtime (late static binding)
  class Counter {
    private static int $total = 0;
    private int $count = 0;

    public function increment(): self {
        $this->count++;
        self::$total++;
        return $this;
    }
}
  

$this in Closures

Anonymous functions do not inherit $this unless bound:

  class Greeter {
    private string $prefix = 'Hello';

    public function greet(string $name): string {
        $fn = function () use ($name) {
            return $this->prefix . ', ' . $name;
        };
        return $fn->bindTo($this, self::class)();
    }
}
  

Arrow functions (fn) automatically capture $this from enclosing scope (PHP 7.4+).

When $this Is Unavailable

  • Static methods — use self:: instead; $this causes a fatal error.
  • Global functions — no object context exists.
  • Closures outside a class — unless explicitly bound with bindTo().

Fluent Interface Pattern

Returning $this enables chainable APIs common in query builders:

  $query = $db->table('users')
    ->where('active', 1)
    ->orderBy('name')
    ->limit(10);
  

Common Pitfalls

  • Passing $this to callbacks that outlive the object can cause unexpected behavior if the object is destroyed.
  • Using $this in a static context is a compile-time error in modern PHP.
  • Confusing $this->property with self::$property for static fields.