self or $this in PHP: When to Use What? Breakdown with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why Does This Question Bother Every Second PHP Developer?

\n\n

If you've ever written PHP in an object-oriented style, you've definitely faced the dilemma: when to use self and when to use $this? This question is one of the most popular on Stack Overflow (thousands of views and dozens of votes). It seems the difference is obvious, but dig a little deeper — and even experienced developers start to get confused.

\n\n

In this article, we'll break everything down. You will learn:

\n
    \n
  • How self and $this fundamentally differ;
  • \n
  • In which situations each one is mandatory;
  • \n
  • How inheritance and static contexts work;
  • \n
  • And, of course, you'll see real code examples you can apply right away.
  • \n
\n\n

Let's figure it out once and for all!

\n\n

Basic Rule: Static vs Instance

\n\n

The most important thing to remember:

\n
    \n
  • $this — refers to the current object (instance of the class). Only works in a non-static context.
  • \n
  • self — refers to the current class where the code is written. Used to access static properties, constants, and static methods.
  • \n
\n\n

Sounds simple? Let's test it with an example.

\n\n

Example 1: Simple Class

\n
<?php\nclass Coffee\n{\n    public static $type = 'Arabica';\n    public $sugar = 2;\n\n    public function info()\n    {\n        // $this — for object properties\n        echo "Sugar: {$this->sugar} spoons.";\n\n        // self — for static property\n        echo "Coffee type: " . self::$type;\n    }\n}\n\n$cup = new Coffee();\n$cup->info(); // Outputs: Sugar: 2 spoons. Coffee type: Arabica\n?>
\n\n

Everything makes sense here: $this->sugar is unique for each cup, and self::$type is common to all.

\n\n

The Trickiness of Inheritance: self vs static

\n\n

This is where it gets interesting. Many think self behaves polymorphically, but it doesn't. self always points to the class where it is written, even if you call the method from a child class.

\n\n

If you need the code to consider the class from which it was called (late static binding), use static.

\n\n

Example 2: Inheritance and the Pitfall

\n
<?php\nclass Animal\n{\n    public static $sound = '...';\n\n    public static function speak()\n    {\n        // NOTE: self is Animal, not Dog!\n        echo self::$sound;\n    }\n\n    public static function speakCorrect()\n    {\n        // static is the class that called the method\n        echo static::$sound;\n    }\n}\n\nclass Dog extends Animal\n{\n    public static $sound = 'Woof!';\n}\n\nDog::speak();       // Outputs: ... (because self = Animal)\nDog::speakCorrect(); // Outputs: Woof! (because static = Dog)\n?>
\n\n

This example is a common source of bugs. If you are writing a library and expect child classes to be able to override static properties, always use static instead of self.

\n\n

When is self Indispensable?

\n\n

There are situations where $this simply doesn't work, and self is the only option:

\n\n
    \n
  • Static methods: inside a method declared as public static function, there is no object, so $this is unavailable. Only self.
  • \n
  • Class constants: they are accessed via self::CONSTANT (or ClassName::CONSTANT).
  • \n
  • Static properties: self::$property.
  • \n
  • Inside a constructor or destructor: although $this is available there, it's better to use

Blogs

Book Recommendations