On Thu, Apr 30, 2026 at 5:10 PM Larry Garfield <[email protected]>
wrote:

> class User {
>   friend UserFactory;
>
>   public friend(set) string $name;
> }
>
> class UserFactory {
>   public function makeUser($name) {
>     $u = new User();
>     $u->name = $name; // This is OK, because UserFactory is a friend.
>     return $u;
>   }
> }


This seems like an overcomplication, in current PHP this would work just
fine:

class User {
    public function __construct(
        public readonly string $name,
    ) {}
}

Some of C++ OOP features have always felt like kludges to me, and
friendships are a good example. A well-written class shouldn't care what
other classes are using it, as long as they respect its contract.

-- 
Best regards,
Max Semenik

Reply via email to