Hi!

On Thu, Jun 4, 2026 at 2:15 AM Daniel Scherzer
<[email protected]> 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, 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.
OCP: the list of friends will be hardcoded inside the User class with
no possibility to change/extend that.
LSP: already covered.
DIP: the fact that User defines its friends is already a soft cyclic dependency.

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.

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".

Regards,
Alexandr Egorov.

Reply via email to