PHP DISCUSSION

PHP self, static and $this inside a class: which one refers to what, and when does it matter?

Started by siva PHP self vs staticlate static binding$thisstatic propertiesclass constants
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

PHP self, static and $this inside a class: which one refers to what, and when does it matter?

siva PHP Forum
#1

I have a base class Model with a static factory create() that does return new self();, and a subclass Reading extends Model. Calling Reading::create() gives me a Model object, not a Reading. I am also unsure when to write self::$count and when $this->count, because both seem to run without a fatal error.

What do $this, self and static each refer to?

Community replies 4

Re: PHP self, static and $this inside a class: which one refers to what, and when does it matter?

#2

$this is the current object and exists only inside a method called on an instance. It reaches instance properties and methods: $this->value, $this->save(). self is the class in which the code is written, and with :: it reaches things that belong to the class and not to any one object: static properties (self::$count), static methods (self::helper()) and constants (self::MAX). Inside a static method there is no object, so using $this there throws an error.

Re: PHP self, static and $this inside a class: which one refers to what, and when does it matter?

#3

Your factory problem is the difference between self and static. self is resolved where the code is written: inside Model it always means Model, no matter which subclass the call came through. static is resolved at runtime to the class that was actually called, a feature named late static binding. Change the factory to return new static(); and Reading::create() returns a Reading.

On PHP 8.0 and later you can also declare it: public static function create(): static, which tells tools and readers that the return type follows the called class.

Re: PHP self, static and $this inside a class: which one refers to what, and when does it matter?

#4

The same distinction applies to constants and static methods that subclasses override. If Model defines const TABLE = 'models' and Reading redefines it as 'readings', a method in Model that uses self::TABLE always sees 'models', while static::TABLE sees 'readings' when called on a Reading.

Use self when you want exactly this class's definition, and static when subclasses are meant to customise it. parent:: is the third member of the family and refers to the class being extended, typically as parent::__construct(...).

Re: PHP self, static and $this inside a class: which one refers to what, and when does it matter?

#5

About $this->count versus self::$count: they are different storage. A static property has one value shared by the whole class; an instance property has one value per object. Writing $this->count = 5 when count is declared static does not update the static property, and PHP reports a notice about accessing a static property as non-static, which is easy to miss if notices are hidden. Run with error_reporting(E_ALL) while developing.

A static counter of created objects is self::$count++ in the constructor. With three objects created, Model::$count is 3 and there is exactly one copy of that number.

TEP COMMUNITY