Re: Pre-RFC discussion about adding friendship to PHP

From: Date: Thu, 07 May 2026 16:30:10 +0000
Subject: Re: Pre-RFC discussion about adding friendship to PHP
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-130795@lists.php.net to get a copy of this message
On Thu, Apr 30, 2026 at 5:10 PM Larry Garfield <larry@garfieldtech.com> 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

« previous php.internals (#130795) next »