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?
On Wed, Jul 8, 2026 at 1:08 AM Holly Schilling <
holly.a.schilling@outlook.com> 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.
Le 08/07/2026 à 14:07, Michael Morris a écrit :
On Wed, Jul 8, 2026 at 1:08 AM Holly Schilling
holly.a.schilling@outlook.com 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.
I think that Swift's extension system goes the other way round: the
class itself doesn't declare its extensions, but they are declared from
userland.
It's similar to how Rust's traits system can allow extending other types
(though there's kind of a safeguard to avoid extending nonsense) , you
can check it there: https://doc.rust-lang.org/book/ch10-02-traits.html
PHP with inheritance goes this way:
- Declare Class A
- Class A internally defines which class it inherits from
- Class A internally defines which traits it uses - Everything is now defined for Class A and its structure is finite as
of the end of its declaration
With an "extension" system, it might go this way:
- Declare Class A
- Class A internally defines which class it inherits from
- Class A internally defines which traits it uses - Class A is defined, but extensions can update it somehow.
- An extension is later declared for Class A: it can customize the inner
structure of Class A, like new traits or implemented interfaces - Maybe (depending on how the RFC goes) other extensions can be declared
later, in order to customize other things.
Conceptually, I'm not against it, but just like Michael said: I can't
find use-cases that would be better than what we actually have.
Extending a class "from the outside", whatever the way to do it, implies
that a new safeguard has to be added. We could use the "final" keyword,
but "final" is not at all the same thing as "no extension allowed": a
class could be declared as not-final while still disallowing
extensibility from the outside. That's the case when you have a
non-final class but some of its methods are "final". Disallowing
extensibility would require an entirely new keyword for that, and it
adds another burden to code maintainers, especially if extensibility is
opt-out.
So, IMO, such RFC should contain this:
- Thorough examples of the actual problems it solves for package
maintainers and application developers - Clear views on how extensibility impacts the current ecosystem (I
guess it shouldn't have any impact on Composer and the autoloading
system, because if it's similar to Swift, an extension should just
behave like a class in terms of autoload, but I might have not thought
about it completely) - Extensibility must be either opt-in by design, or opt-out by design,
but the decision must be made after having analysed the impact on
the ecosystem. I can't imagine extensibility being opt-out and people
starting to wildly override frameworks core classes. (yeah, I'm talking
about some Laravel nerds that tend to bend and break fast (pun intended)) - Extension points must be defined on both the extender and the extended
structures. Like, for example, making sure it's impossible to extend
something that's "final" by default, but making a non-final structure
being non-extensible should also be possible. That's part of the concept
of encapsulation in general, but it also respects the Open/Close
principle (from SOLID) and several other programming practices that make
sure we don't do bullcrap in all codebases. And trust me, PHP is already
full of bullcrap, so adding more isn't a good idea at all. PHP hasn't
become "more and more strict" over time for nothing... and still, that
strictness is opt-in (by the eyes of PHP, not from framework
maintainers, which can be seriously opinionated).
Hope this helps :)
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
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?
Hi Holly,
Can you give a brief summary of what the feature is, and how it would apply to PHP, for those of us who aren't familiar with Swift, and haven't seen the Discord discussion?
Thanks,
Rowan Tommins
[IMSoP]
Hey Holly,
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?
I like how extensions in Swift allow to organise code!
If we ignore implementation details and ownership it's pretty similar to
what we have with traits. In PHP, what would be the benefit of
extensions over traits? One thing that comes to my mind is that traits
can be used by whatever chooses to add it (back to ownership). Good one,
but what else? If ownership is the only concern then maybe a small
addition to traits could achieve the same?
trait Foo
{
for Baz\Bar::class; // inverse of "use" to limit usage
}
class Bar
{
use Foo;
}
Brings conflict resolution for free; and I think it would be cheaper
than yet another class-like thing. Maybe also feels more php-ish since
we already have the trait concept?
Before I go into much more detail, what is the general interest in
adding this to the language?
Interested in some sort of solution!
--
Cheers
Nick