Re: Class Extension
| From: | Holly Schilling | Date: | Fri, 10 Jul 2026 11:53:39 +0000 |
| Subject: | Re: Class Extension | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-131853@lists.php.net to get a copy of this message | ||
>> The reason I don’t feel the
trait … for syntax is
>> appropriate is because of $this. In a function inside a typical
>> trait, $this refers to an instance of the trait. In an extension,
>> $this refers to an instance of the class being extended.
>
>
> I don't think that's true - there's no such thing as an "instance of a
> trait", the contents of a trait are pasted directly into the class where they're used, and
> act just as if they were written inline at that point.
>
> What that does mean is that they have access to private members, which I understand extensions
> would not, so that might be a good reason to use a new keyword.
>
>
> Rowan Tommins
> [IMSoP]
I was unclear in my description. $this in a trait will be an instance of what it
applies to and will pass runtime checks via instanceof. What I was referring to is that
at compile-time from inside a function of the trait, it can only call members known to the trait.
Whereas in an extension, the entire public surface of the extended type is known and available, plus
the members of the extension.
So that isn’t to say trait … for couldn’t work, but it feels very different to a
trait in its use.