Re: Class Extension
| From: | Holly Schilling | Date: | Fri, 10 Jul 2026 18:33:59 +0000 |
| Subject: | Re: Class Extension | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-131861@lists.php.net to get a copy of this message | ||
.
>
> As far as scalar methods goes, I hate my implementation. I’m not suggesting someone else
> could do better, only that the compromises necessary to implement it weren’t worth it. After
> having implemented it and seen what it demands, I think the right choice is Autoboxing with
> extensions on the box rather than ever allowing
$this to not be an
> object.
>
> This is a heated topic lately and I don’t pretend to be an expert.
>
Typing this email this morning gave me real hesitation. If I can’t support my own implementation,
no one else should either. I immediately set out to build a better version that I could put my full
weight behind.
Autoboxing turned out to be a non-starter as well. The code just didn’t fit the model of the code
PHP uses. That led me to a diffident path.
C# 14 recently added a new extensions syntax to allow for properties and more. When I saw it, I knew
it was the right solution.
```
extension string $str {
function length() {
return strlen($str);
}
}
var_dump(“test”->length()); // int(4)
```
By declaring the parameter inline with the extension declaration, we eliminate the downcast of
$this, the fake accessibility constraints, and all the sharp edges the old way
introduces.
I immediately rewrote all 3 RFCs to use this new syntax, as well as the code to implement it.
Anyone who has been following these discussions, please skim the updated docs for changes.