Re: Re: [RFC] [Discussion] Friends
| From: | Alexander Egorov | Date: | Sat, 21 Nov 2026 05:17:54 +0000 |
| Subject: | Re: Re: [RFC] [Discussion] Friends | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-133162@lists.php.net to get a copy of this message | ||
Hi! Thanks for the reply!
On Sat, Nov 21, 2026 at 2:26 AM Daniel Scherzer
<daniel.e.scherzer@gmail.com> wrote:
>
> The "S" in SOLID is for the single responsibility principle - when the User class
> changes other things shouldn't need to change too. But, the use case for friends is for when
> this is *already being violated* by using reflection, or (ab)using the public API by having internal
> methods be public. This is just making things clearer.
>
Not really. Single responsibility is about the zone (scope) of
responsibility of the class (what problem it solves), not about
changing or not the contract. The User's responsibility is to model
the user in some way - that is its single responsibility. Gating
access to any of its methods, if needed, either public or not, is a
separate scope, and architecturally must be external to the User
according to SRP.
We first had a User class, which is already self-sufficient for the
goal of modelling a User, then we decided we need a factory for
instantiating a User - a separate scope of responsibility. And
according to SRP, we would only need to create the Factory class. In
your solution, we would also need to modify the User class, despite
that nothing about modelling the User itself changed. That's what
breaks the SRP principle already. But let's assume for the moment
we're OK with that. It leads to another problem. If we then decide to
add a UserRepository class, which would also require access to some
internals of User, we would again need to modify the User class. So,
here comes the violation of OCP.
> That is because this is an internal detail of the User class! OCP is that classes should be
> "open to extension, but closed for modification" - friendship falls under modification,
> not extension, because friends are not in the same type hierarchy. Thus, OCP suggests that the list
> of friends *should* be hardcoded without a way to change it at runtime.
>
Like, literally, instead of extending the list of possible
"intruders", we're always modifying the User class.
> There is future scope for supporting abstractions as friends, per the RFC
> future scope includes
And that's exactly the problem which an abstract solution could solve
as one of possible solutions. Instead of always modifying the User for
each new "intruder", it could just define such access as an
abstraction, and any other future client would not need to be
hardcoded inside the User, but instead only implement the abstraction
in any way. Like, in C#, for example, it is the "internal" thing,
which is an abstract declaration of who can access protected methods.
Technically speaking, "public/private/protected" is already a similar
abstraction: it defines access levels on the abstract level without
knowing anything about the exact consumers of the class.
> ... and if it was using reflection, the class was implicitly supporting manipulation that went
> through reflection rather than the stable public interface. This just switches that implicit support
> to something explicit.
Not really. If any other class uses reflection or bound closures to
access the internals of User and rely on them, it is completely beyond
the scope of User's responsibility (otherwise SRP is already broken).
As I've mentioned in my previous message, it is the sole
responsibility of the consumer if it decides to use such an approach,
taking all the risks. Meanwhile, even though intruding into class's
internals is not a good thing, doing it from external is a cleaner
solution according to considerations above, than going to User itself
and modifying it. In the end, if it all happens inside the same
project/package, it already means something has to be refactored
instead of kludging.
>> It could look similar to using the traits, for example:
>>
>> class UserFactory
>> {
>> accesses User; // or it could be specific methods inside { ... }
>> }
>>
>> Or maybe even:
>>
>> class UserFactory internal to User, SomeOtherClass // exact word is
>> TBD, and correspond to current PHP reserved words/lexer.
>
>
> While this could be done, there is a benefit to the friendship being declared *on the User
> class* - it tells developers when changing the User class that there is another class (here the
> UserFactory) that is coupled with the internal details of the User class. If friendship was declared
> on UserFactory instead, then developers changing the "internal" details of the User class
> are less likely to see that UserFactory also needs to be updated and just hope that tests catch the
> mistake.
Well, changing the contracts of classes happen now and then internally
to the project, and it always entails discovering the consumers. But
it is never a responsibility of the class itself to know about all of
its consumers. Such linkage from the class to consumers is exactly
what breaks the architecture, no matter if those links are hard or
soft, like comments or some meta-information, like attributes. If such
consumers can't be easily discovered, that is a problem of another
kind, not related to the "friendship" mechanic.
> I'm unclear why you consider friendship too vague, since it is the term used in other
> languages (c.f. https://en.wikipedia.org/wiki/Friend_class) or why
> it would be considered vulgar.
By that I meant that since we're talking about a formal programming
language, the term should be more technical from my pov. Specifically
the "Friend" term is used only inside C++. Any other languages which
give similar mechanics don't use it: "internal" in C#, "fileprivate"
in Swift, etc.
> -Daniel
Regards,
Alexandr Egorov.