> On Jul 8, 2026, at 14:07, Michael Morris <[email protected]> wrote:
>
>
> On Wed, Jul 8, 2026 at 1:08 AM Holly Schilling
> <[email protected]> wrote:
> Following the discussion in Discord yesterday, I started working on the
> feasibility of implementing class extensions. I believe I’ve proven out the
> concept and viability of it in a 2-phase approach using an extension syntax
> similar to that of Swift.
>
> Before I go into much more detail, what is the general interest in adding
> this to the language?
>
> We have inheritance (extends keyword, parent / child classes)
> We have traits (an implementation of multiple classes taken from Ruby)
>
> Looking at Swift's extension syntax I fail to see anything it adds not
> covered by the above.
Traits and extensions approach a similar problem from the opposite direction.
Where a class uses a trait to add functionality, instead, an extension declares
that it has added functionality to a class (or interface).
Besides allowing one avenue for default implementations for interface methods,
extensions would also allow for polyfills for class methods and properties that
can be hooked. For example, PHP 8.5 adds the (sadly not documented yet!)
`Dom\Element::getElementsByClassName()` and `Dom\Element::insertAdjacentHTML()`
methods. If PHP 8.4 already had extensions, then extensions would have made it
possible for a userspace library to provide an implementation for those methods
on PHP 8.4.
In the hypothetical case where we have method calls on scalar types, extensions
could also be used as a means of adding methods to those types (either in
userspace, or internally or in php extensions).
-John