self or $this in PHP: when and what to use? Complete guide

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

When to use self and when to use $this? Let's figure it out once and for all

\n\n

If you've ever written PHP in an object-oriented style, you've probably come across the question: "So, what should I choose — self or $this?" This question is so popular that it has gathered tens of thousands of views and hundreds of comments on Stack Overflow. And this is no coincidence: the difference between them is one of those "little things" that can turn your code into something that works but is not obvious, or into something elegant and understandable.

\n\n

Let's agree right away: these are not interchangeable things. They have a different philosophy and, more importantly, different behavior during inheritance. If you use $this where you need self, or vice versa, you risk getting logical errors that are difficult to catch.

\n\n

In this article, we'll break everything down: from the basics to advanced scenarios. Let's go!

\n\n
\n\n

Short answer (for those in a hurry)

\n\n

If you want to remember just one rule, remember this:

\n\n
    \n
  • $this-> — for accessing instance properties and methods (of an object). Only works within a non-static context.
  • \n
  • self:: — for accessing static properties and methods of a class. Works in both static and non-static contexts.
  • \n
  • static:: — "Late Static Binding". Used when you need a child class to override the parent's behavior.
  • \n
\n\n

But the devil, as always, is in the details. Let's dive deeper.

\n\n

What is $this?

\n\n

$this is a pseudo-variable that refers to the current object. It is only available inside class methods (non-static) and points to the specific instance you are working with.

\n\n
<?php\n\nclass User {\n    public string $name;\n\n    public function setName(string $name): void {\n        $this->name = $name; // accessing the property of the current object\n    }\n\n    public function getName(): string {\n        return $this->name;\n    }\n}\n\n$user = new User();\n$user->setName('Anna');\necho $user->getName(); // Anna
\n\n

It's simple: $this->name means "get the name property of the specific object that called this method".

\n\n

What is self?

\n\n

self is a reference to the current class, not to an object. It is used to access static members of a class (properties and methods declared with the static keyword).

\n\n
<?php\n\nclass Config {\n    public static string $appName = 'PythonLib';\n\n    public static function getAppName(): string {\n        return self::$appName; // accessing the static property of the class\n    }\n}\n\necho Config::getAppName(); // PythonLib
\n\n

Note: self always refers to the class in which it is written. Even if you create a child class, self in the parent class will continue to point to the parent. This is a crucial difference from static.

\n\n

Main difference: self vs $this during inheritance

\n\n

This is where things get interesting. Imagine you have a base class Animal and a child class Dog.

\n\n
<?php\n\nclass Animal {\n    public static function whoAmI(): string {\n        return 'I am an animal';\n    }\n\n    public function say(): string {\n        // Using self — it is bound to the Animal class\n        return self::whoAmI();\n    }\n}\n\nclass Dog extends Animal {\n    public static function whoAmI(): string {\n        return 'I am a dog';\n    }\n}\n\n$dog = new Dog();\necho $dog->say(); // I am an animal

                
                

Blogs

Book Recommendations