Extension methods are a proposed PHP feature that lets you add methods to a class you don't own, like \DOMElement, and a second RFC controls who can see them. You bring an extension into a file with a use extension line, but importing it doesn't load the file that declares it. You still need a require_once or a Composer autoload.files entry. This RFC from Holly Schilling removes that step by letting PHP's normal class autoloader load the file the first time you need it.

Show me

A package declares an extension in its own file:

namespace Acme\DomKit;

extension DomTraversal on \DOMElement $el {
    public function firstByClass(string $class): ?\DOMElement { /* ... */ }
}

Your code only writes the use extension line:

use extension Acme\DomKit\DomTraversal;

$el->firstByClass('hero');   // first use: autoloaded, resolved, called

Nothing else loads DomTraversal.php. The autoloader finds it the same way it finds a class.

How it works

  • Named extensions go in the class table, the list of classes PHP knows about. class_exists('Acme\DomKit\DomTraversal') returns true once it's loaded.
  • Names can't clash. An extension can't share a name with a class, interface, trait, enum or another extension. That's a fatal error.
  • It isn't a class. Using new on an extension name throws an error.
  • Loading happens on first use. When a method call can't be resolved, PHP tries to load every extension the file imported but hasn't loaded yet, then looks up the method one more time.
  • Each name gets one attempt per request. An import that can't be loaded isn't an error, just like an unused use for a class.
  • Loading doesn't change visibility. Only files that import an extension can call its methods.

The author acknowledges one cost. If you typo a method name, PHP loads all of that file's unloaded imports before it reports the error.

A draft Composer pull request already maps extension declarations to their files.

What it means for existing code

Nothing changes for code that doesn't use extensions. Extension methods haven't shipped yet, so this RFC only changes how that proposal would work.

Where it stands

The RFC is a draft, with no discussion or vote thread set yet. It depends on the Extension Method Visibility RFC, and if that one is declined, this vote doesn't count. It would ship in the same release as the base Extension Methods RFC.