New self vs. new static

New self vs. new static

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

New self vs. new static

Converting a PHP 5.3 library to run on PHP 5.2 requires more than just updating syntax; it demands a fundamental shift in how you handle class instantiation within factory methods or constructors. The specific hurdle you are facing—replacing return new static($options); with return new self($options);—touches on one of the most distinct behavioral differences between late static binding and standard inheritance resolution.

If your goal is simple compatibility where every call to a factory method returns an instance of the class where that method was defined, you might be tempted to swap these keywords immediately. However, doing so breaks the contract established by polymorphism, which is often the primary reason developers adopt late static binding in the first place.

The Mechanism of Late Static Binding

In PHP 5.3 and later, new static invokes the constructor of the class that was statically bound at the time of the call, not necessarily the class where the code resides. This behavior relies on the current scope's resolved class. If you have a hierarchy like Model, ChildModel, and GrandchildModel, calling a factory method on GrandchildModel will return an instance of GrandchildModel when using new static.

class Model {
    protected $type;

    public function __construct($type) {
        $this->type = $type;
    }
}

class ChildModel extends Model {
    public static function create($options) {
        // PHP 5.3+ behavior: Returns instance of the calling class
        return new static($options);
    }
}

$child = ChildModel::create(['id' => 1]);
echo get_class($child); // Outputs: ChildModel

When you switch to new self, the logic changes entirely. The keyword self is resolved at compile time relative to the class where the code physically lives. In the example above, return new self($options) inside ChildModel will always instantiate a ChildModel, regardless of whether the method was called via ChildModel::create() or GrandchildModel::create().

class ChildModel extends Model {
    public static function create($options) {
        // Always returns ChildModel instance
        return new self($options);
    }
}

class GrandchildModel extends ChildModel {
    // Overrides the method to use late static binding
    public static function create($options) {
        return new static($options);
    }
}

$grandChild = GrandchildModel::create(['id' => 1]);
echo get_class($grandChild); // Outputs: GrandchildModel

// If you changed this to 'new self', it would output ChildModel here, breaking the inheritance chain.

The PHP 5.2 Constraint

Your specific constraint of targeting PHP 5.2 introduces a hard stop for static in factory methods if you intend to preserve polymorphic behavior across derived classes. In PHP 5.2, new static is not supported; the language only recognizes self, parent, and the fully qualified class name.

If your original PHP 5.3 library relies on new static to ensure that extending classes get their own specific instances without overriding every single factory method in the hierarchy, you cannot simply use new self as a drop-in replacement. Doing so forces every subclass to explicitly override the factory method if they want to return an instance of themselves rather than the parent class.

For a strict PHP 5.2 migration where you must avoid static, the trade-off is explicitness over abstraction. You have two viable paths:

  1. Explicit Class Names: Replace new static with the fully qualified class name inside each subclass.

    // In ChildModel.php
    public static function create($options) {
        return new self($options); // Works, but returns ChildModel specifically
    }
    
    // In GrandchildModel.php, you must override:
    public static function create($options) {
        return new GrandchildModel($options); // Explicitly name the class
    }
    
  2. Interface Segregation: If the library architecture allows, define an interface for the creation method and let concrete classes implement it with new self. This shifts the responsibility of polymorphism to the implementation rather than relying on language magic that isn't available in 5.2.

When to Use Which

If your PHP 5.3 code uses new static purely to avoid repeating class names in every subclass, you are trading convenience for a feature set that didn't exist in PHP 5.2. The decision to migrate to new self is acceptable only if:

  • The hierarchy is shallow (few levels of inheritance).
  • Every subclass overrides the factory method to return its own type anyway.
  • Polymorphic instantiation based on runtime context is not a requirement for your specific use case.

If you find that converting new static to new self requires adding numerous method overrides across your codebase, the effort might outweigh the benefits of supporting PHP 5.2. In many modern contexts, sticking with PHP 5.4 or higher allows you to retain the elegance of late static binding without significant refactoring costs.

For further reading on PHP version differences and best practices for legacy migrations, resources like https://laravelcompany.com often provide detailed guides on maintaining codebases across versions while adhering to modern standards.

Our network