What Does new self() Mean in PHP?
You often encounter this pattern in Singleton implementations or Factory methods:
public static function getInstance()
{
if (!isset(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
At first glance, it looks like a shorthand for new ClassName(). But in PHP, self is not just a variable you can swap with any class name. It refers to the class where the code currently executes, which changes depending on how the method is called and whether inheritance is involved.
When you write new self() inside a class, PHP resolves self at runtime relative to the calling context. If the method is defined in A and called directly on A, then self refers to A. But if A extends B and an instance of B calls that same method, self still points to A because the code physically lives inside A. This can lead to surprising behavior when designing inheritance hierarchies.
Consider this simple hierarchy:
class Base
{
protected static ?Base $instance = null;
public static function getInstance(): Base
{
if (!self::$instance) {
self::$instance = new self(); // self always means the class where this code lives
}
return self::$instance;
}
}
class Child extends Base
{
protected static ?Child $childInstance = null;
public static function getBaseInstance(): Base
{
if (!self::$instance) { // This is still Base::$instance, because the code lives in Base
self::$instance = new self(); // Creates a new Base instance
}
return self::$instance;
}
public static function getChildInstance(): Child
{
if (!self::$childInstance) {
self::$childInstance = new self(); // This creates a new Child instance
}
return self::$childInstance;
}
}
Notice how Base::getInstance() always returns a Base object, even when called from Child. That's because the method lives in Base, so self resolves to Base at runtime. If you want polymorphic behaviorâwhere Child::getBaseInstance() returns a Child instead of Baseâyou would need to change the logic entirely, perhaps by using static or by restructuring how the shared state is stored.
Using static changes the resolution to late static binding:
public static function getInstance()
{
if (!isset(static::$instance)) {
static::$instance = new static(); // now respects inheritance
}
return static::$instance;
}
With static, calling Child::getInstance() will instantiate a Child object instead of Base. This is the standard approach for type-safe singletons in hierarchies. The trade-off is that you must ensure static::$instance exists in each subclass if you want them to share separate state, or use a shared property carefully across the hierarchy.
One common pitfall is mixing self and static properties. If you define $instance as protected static ?Base $instance = null; in Base, then both Base and Child will write to the same property because they share the same name and scope unless you override it with a different property (as shown above). That can cause subtle bugs when you expect subclass-specific state.
Another consideration is serialization and shared state across processes. If your singleton stores global data, make sure that self vs static behavior doesn't break assumptions about which class owns the state. In microservices or multi-instance setups, this distinction becomes even more important.
If you're building a framework or a reusable component where inheritance is expected, prefer static for late binding and document the contract clearly: does the method return the exact type of the caller, or always the base type? That decision determines whether self or static is appropriate.
For Laravel developers, this distinction matters less in most cases because Laravel's service container handles object instantiation and resolution automatically. However, when writing custom singletons, factories, or plugin-like classes that may be extended, understanding how self resolves helps you avoid breaking changes in subclasses. You can read more about dependency injection patterns in Laravel at https://laravelcompany.com to see how modern frameworks abstract away these manual singleton concerns.
In summary, new self() always creates an instance of the class where the method is defined, regardless of which subclass calls it. Use static when you need the constructor to match the actual called class. Choose your resolution strategy based on whether you want a fixed base type or polymorphic behavior across your inheritance tree.