Introduction: Why Does This Question Bother Every Second PHP Developer?
\n\nIf 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.
In this article, we'll break everything down. You will learn:
\n- \n
- How
selfand$thisfundamentally 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
Let's figure it out once and for all!
\n\nBasic Rule: Static vs Instance
\n\nThe 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
Sounds simple? Let's test it with an example.
\n\nExample 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\nEverything makes sense here: $this->sugar is unique for each cup, and self::$type is common to all.
The Trickiness of Inheritance: self vs static
\n\nThis 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.
If you need the code to consider the class from which it was called (late static binding), use static.
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\nThis 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.
When is self Indispensable?
\n\nThere are situations where $this simply doesn't work, and self is the only option:
- \n
- Static methods: inside a method declared as
public static function, there is no object, so$thisis unavailable. Onlyself. \n - Class constants: they are accessed via
self::CONSTANT(orClassName::CONSTANT). \n - Static properties:
self::$property. \n - Inside a constructor or destructor: although
$thisis available there, it's better to use