Re: Re: [RFC] [Discussion] Friends

From: Date: Fri, 20 Nov 2026 23:26:14 +0000
Subject: Re: Re: [RFC] [Discussion] Friends
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-133159@lists.php.net to get a copy of this message
Hi Alexandr, On Sun, Nov 15, 2026 at 4:26 PM Alexander Egorov <amego2006@gmail.com> wrote: > Hi! > > On Thu, Jun 4, 2026 at 2:15 AM Daniel Scherzer > <daniel.e.scherzer@gmail.com> wrote: > > > Seeing no further feedback, I have adjusted the RFC and the > implementation to only allow access to protected > properties/methods/constants. > > This qualifies as a "major change" and triggers a 14 day cooldown period. > > Thanks for RFC, gave it a look and want to add to other's concerns. > Even though the goal of this RFC sounds useful, I strongly believe it > should be fully inverted: not User declares its friends, but all the > clients get access to the internals if they need. Many have pointed > out the violation of LSP, The violation of LSP was about access to private properties, which has been removed in the latest version of the RFC. > but I would add that the current variant > actually violates most of SOLID principles. It has been implicitly > stated already by other commenters in this or previous thread, but I'd > like to elaborate on this. > > SRP: if User declares UserFactory as a friend, it means that for some > reason it already knows why the factory needs it. In other words, it > goes beyond its responsibility. > 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. > OCP: the list of friends will be hardcoded inside the User class with > no possibility to change/extend that. > 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. > LSP: already covered. > DIP: the fact that User defines its friends is already a soft cyclic > dependency. > There is future scope for supporting abstractions as friends, per the RFC future scope includes > Adding support for inherited friendship (“UserFactory and all of its subclasses are all friends of User”) > Adding support for namespaces as friends (“Any class in \My\Library\Namespace is a friend of User”) but, I'll note that switching to friends *should not introduce new dependencies* - if the existing code was making things public, it was going against other principles, 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. > > If we look from another angle, a User declaring its friends is > semantically the same as if it described some of its inheritors, which > would also vialote many concerns: a class must know neither its > clients nor possible inheritors. The only thing it could do is declare > such things abstractly. > > So, instead, as I've said, I would go for a fully inverted solution: > the clients themselves declare that they are accessing some other > class' internals, and it would be purely the client's responsibility > if that solution is OK architecturally. > > 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. > > In this case this "friendship" would just become a sugar for how we do > these things currently anyway using reflection and/or bound closures. > But honestly, I would avoid using the word "friendship" for this > mechanic, because it is too vague or probably even "vulgar". > 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. > > Regards, > Alexandr Egorov. > -Daniel

« previous php.internals (#133159) next »