PHP self vs $this: Difference with Practical Examples

At first, self and $this can look like two different ways to reach something inside the same PHP class. They are not.

The distinction usually stays hidden until inheritance enters the code. Then a harmless-looking change from $this->method() to self::method() can make PHP call a different method. That is the kind of bug that makes you stare at perfectly valid code for a little too long.

In PHP, $this refers to the current object instance. self refers to the class where the code is defined.

That difference affects properties, methods, static members, and especially inherited classes.

Quick answer: self vs $this in PHP

Use $this when you need the current object’s properties or methods.

$this->name;
$this->getName();

Use self:: when you deliberately want to refer to the class where the current code is defined, commonly for static properties, static methods, and class constants.

self::$count;
self::getCount();
self::DEFAULT_LIMIT;

The common shortcut is to remember $this for instance members and self for static members. That works for many everyday cases, but it is not the complete rule. The important distinction is object instance versus defining class.

Feature $this self
Refers to Current object Class where the code is defined
Typical operator -> ::
Instance properties Yes No
Static properties No Yes
Available in static methods No Yes
Responds to child-class method overrides Yes, for normal method calls No, when self:: fixes the call to the defining class

Using $this for the current object

Each object can hold its own state. $this gives an instance method access to the object on which that method was called.

<?php

declare(strict_types=1);

class Product
{
    public function __construct(
        private string $name,
        private float $price
    ) {
    }

    public function getLabel(): string
    {
        return $this->name . ' - $' . number_format($this->price, 2);
    }
}

$book = new Product('PHP Handbook', 29.50);
$course = new Product('PHP Course', 49.00);

echo $book->getLabel();
echo PHP_EOL;
echo $course->getLabel();

Both objects execute the same getLabel() method. But $this points to a different object each time.

$this->name
$this->price

For $book, those expressions read the book’s values. For $course, they read the course’s values.

This is why $this belongs to an object context. A static method has no current object, so $this is not available there.

Using self for the defining class

self refers to the class where the current method is defined. It is commonly used with static properties, static methods, and class constants.

<?php

declare(strict_types=1);

class Order
{
    private static int $count = 0;

    public const STATUS_NEW = 'new';

    public function __construct()
    {
        self::$count++;
    }

    public static function getCount(): int
    {
        return self::$count;
    }

    public function getInitialStatus(): string
    {
        return self::STATUS_NEW;
    }
}

$orderOne = new Order();
$orderTwo = new Order();

echo Order::getCount(); // 2
echo PHP_EOL;
echo $orderOne->getInitialStatus(); // new

The $count property belongs to the class rather than to each individual object. That is why it is accessed as self::$count.

The constant is also a class-level member, so self::STATUS_NEW is appropriate.

Notice that using self does not mean the containing method must itself be static. An instance method can use self:: to access class-level members.

Why self and $this behave differently with inheritance

This is where the distinction becomes important.

Suppose a parent class has a method that calls another method. A child class then overrides that second method.

<?php

declare(strict_types=1);

class Message
{
    public function sendWithThis(): void
    {
        $this->deliver();
    }

    public function sendWithSelf(): void
    {
        self::deliver();
    }

    protected function deliver(): void
    {
        echo 'Message::deliver';
    }
}

class EmailMessage extends Message
{
    protected function deliver(): void
    {
        echo 'EmailMessage::deliver';
    }
}

$email = new EmailMessage();

$email->sendWithThis();
echo PHP_EOL;
$email->sendWithSelf();

The output is:

EmailMessage::deliver
Message::deliver

The first call uses:

$this->deliver();

$this is the EmailMessage object, so PHP uses the overridden deliver() method in the child class.

The second call uses:

self::deliver();

That call is bound to the class where sendWithSelf() is defined, which is Message. The child override is not selected.

This is the main reason not to think of self as simply a shorter replacement for $this. They express different intent.

Where static:: fits

PHP provides static:: for cases where you need class-style access but still want inheritance to affect which implementation is used. This behavior is called late static binding.

<?php

declare(strict_types=1);

class Report
{
    protected static string $format = 'HTML';

    public static function getFormatWithSelf(): string
    {
        return self::$format;
    }

    public static function getFormatWithStatic(): string
    {
        return static::$format;
    }
}

class PdfReport extends Report
{
    protected static string $format = 'PDF';
}

echo PdfReport::getFormatWithSelf();
echo PHP_EOL;
echo PdfReport::getFormatWithStatic();

The output is:

HTML
PDF

self::$format points to the property as resolved from Report, where the method is defined.

static::$format respects the class used for the call, so PdfReport::getFormatWithStatic() reads the overridden value from PdfReport.

A useful rule is:

  • Use $this when working with the current object.
  • Use self:: when you intentionally want the defining class.
  • Use static:: when subclasses should be able to change class-level behavior.

Common mistakes with self and $this

Most problems come from choosing the operator by habit instead of by intent.

Using $this inside a static method

A static method is called without an object instance. So there is no $this available.

<?php

declare(strict_types=1);

class Counter
{
    private static int $count = 0;

    public static function increment(): void
    {
        self::$count++;
    }
}

This is valid because the method accesses a static property through self::.

Trying to use $this in the same method is invalid:

public static function increment(): void
{
    $this->count++;
}

PHP reports an error because $this cannot be used in static context.

Using self:: when you expect polymorphism

This mistake is more subtle because the code may run without any error.

If a parent method calls another method with self::, that call stays tied to the defining class. A child override will not automatically take over.

So if subclasses are expected to customize the behavior, use $this->method() for instance methods or static::method() for late-bound static behavior.

Using -> and :: interchangeably

The operators reflect two different access styles.

$this->name;
$this->calculateTotal();

self::$count;
self::create();
self::DEFAULT_LIMIT;

The arrow operator -> works through an object. The scope resolution operator :: works through a class scope. PHP also documents the :: operator and its use with constants, static properties, static methods, self, parent, and static.

Keeping that distinction visible in the code makes the intent easier to understand later.

self vs $this in constructors

Constructors often use both forms, which makes them a good place to see the difference clearly.

<?php

declare(strict_types=1);

class User
{
    private static int $createdCount = 0;

    public function __construct(
        private string $name
    ) {
        self::$createdCount++;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public static function getCreatedCount(): int
    {
        return self::$createdCount;
    }
}

$user = new User('Anna');

echo $user->getName();
echo PHP_EOL;
echo User::getCreatedCount();

$this->name belongs to one specific User object. self::$createdCount is shared at the class level.

There is no conflict in using both inside the same class. They simply refer to different things.

Can you call a static method with $this?

PHP may allow some static methods to be reached through an object in certain forms, but that is not a good reason to write code that way.

If a method is static, call it as a class-level method:

self::formatValue();

or from outside the class:

Formatter::formatValue();

This keeps the code aligned with the method declaration and avoids giving readers the false impression that the method depends on object state.

Which one should you use?

Choose based on what the code is meant to refer to.

  • If the value or method belongs to the current object, use $this.
  • If the member belongs to the class and should stay tied to the defining class, use self::.
  • If the member is class-level but child classes should be able to override the behavior, consider static::.

That small distinction makes inheritance much easier to reason about. It also prevents a common PHP problem where code looks correct, runs correctly, and still calls the wrong implementation.

Developer FAQ

Can self access non-static properties?

No. Instance properties belong to an object, so they need an object reference such as $this.

$this->name;

Use self::$property only for a property declared as static.

Can $this be used in a static method?

No. A static method does not run on a particular object instance, so $this is unavailable.

Is self the same as the current class name?

In many situations, yes. Inside a class, self::method() is similar to explicitly using that class name.

The important difference appears when inheritance is involved. self remains tied to the class where the method was defined, while static can resolve to the class that was actually called.

Should I use self:: or static:: in a parent class?

Use self:: when the parent implementation should remain fixed.

Use static:: when child classes should be able to override class-level properties or methods. This is especially useful in reusable base classes.

Is $this available in a constructor?

Yes. A constructor runs for a specific object being created, so $this is available just like it is in other instance methods.

Summary

The simplest way to remember the difference is that $this follows the object, while self stays with the class where the code is defined.

For ordinary instance state, use $this. For fixed class-level references, use self::. When inheritance should influence class-level behavior, use static::.

Once inheritance enters the picture, that distinction matters much more than the usual shortcut of “instance versus static.”

Photo of Vincy, PHP developer
Written by Vincy Last updated: August 12, 2026
I'm a PHP developer with 20+ years of experience and a Master's degree in Computer Science. I build and improve production PHP systems for eCommerce, payments, webhooks, and integrations, including legacy upgrades (PHP 5/7 to PHP 8.x).

Continue Learning

These related tutorials may help you continue learning.

14 Comments on "PHP self vs $this: Difference with Practical Examples"

Leave a Reply

Your email address will not be published. Required fields are marked *

Explore topics
Need PHP help?