New self vs new static in PHP: what to choose in 2025?
Imagine: you've been writing PHP for several years, you feel confident, and suddenly your colleague (or Stack Overflow) asks: "Why does my child class create an object of the parent class, even though I'm calling the constructor via new self()?" Sound familiar? If so, you are not alone. The topic of new self vs new static is one of the most popular questions on Stack Overflow with thousands of views. And in 2025, with the release of new PHP versions, understanding this difference has become even more important.
In this article, we will figure out how self and static differ when creating objects, how Late Static Binding works, and most importantly, when to use which. Let's go!
The problem: self always points to the parent
Let's start with a classic example. Suppose we have a parent class BaseController and a child class ApiController.
<?php
class BaseController { public static function create() { return new self(); // Attention! self is BaseController }}
class ApiController extends BaseController { // Inherits create()}
$api = ApiController::create();var_dump($api); // object(BaseController)#1 (0) { }?>We expected to get an ApiController object, but we got BaseController. Why? Because self in the context of the parent class always refers to the class where it is written, not the class through which it was called. This is early static binding. PHP "remembers" the class at compile time, not at runtime.
Solution: use static
To fix the situation, you need to replace self with static. This enables the late static binding mechanism (LSB).
<?php
class BaseController { public static function create() { return new static(); // static is the class that called the method }}
class ApiController extends BaseController { // Inherits create()}
$api = ApiController::create();var_dump($api); // object(ApiController)#1 (0) { }?>Now $api is an ApiController object. static is resolved at runtime and points to the class through which the method was called. This is late static binding.
When to use self and when to use static?
In 2025, best practices clearly separate these two approaches. Here are the basic rules.
Use self when:
- You are working within a single class and do not expect inheritance for this method.
- You need to create an object of exactly the class in which the code is written, not a child class.
- You are accessing constants or static properties that should not be overridden.
Example: a factory method that guarantees returning an object of the base class.
<?php
class Logger { protected static $instance;
public static function getInstance() { if (self::$instance === null) { self::$instance = new self(); // Always Logger } return self::$instance; }}?>Use static when:
- You are designing code for extension — for example, abstract classes or traits.
- You need child classes to be able to create their own instances.
- You are using the Factory Method or Builder pattern.
Example: an abstract class for entities.
<?php
abstract class Entity { public static function create(array $data): static {\