On Mon, Mar 24, 2025, at 3:47 AM, Rob Landers wrote:

> On Sun, Mar 23, 2025, at 16:17, Larry Garfield wrote:

>> I've been following this thread with interest, and at the moment I'm 
>> honestly undecided.  I certainly see the use cases for this functionality 
>> (whatever it gets named), but as a practical matter it sounds like it 
>> introduces a lot of extra clunk and complexity.  And it seems like the use 
>> cases could be addressed as well with either fileprivate or module-private.  
>> (The former being considerably less work.)
>> 
>> So, how would nested classes compare to fileprivate, in terms of ability to 
>> solve the problem space?  As I understand it, the goal is:
>> 
>> 1. Classes that can be instantiated only by the class that uses them.
>> 2. But can be returned from that class to a caller and reused as appropriate.
>> 
>> The autoloading question (loading a whole file for just an implementation 
>> detail value object) is not one that carries much weight for me, as that's a 
>> user-space question, not an engine question.  (Nothing in PHP itself says 
>> you cannot put 20 3 line classes or enums together in one file.  It's just 
>> PSR-4 that says not go. Even composer would allow it if configured properly) 
>>  So how would the less-complicated alternative compare?
>> 
>> --Larry Garfield
>
> Hey Larry,
>
> I think file-private would/could be useful, but that only limits you to 
> a "private" scope, which severely hampers what you can do with it. If 
> we went with "module-private" (rhetorical question: what is a module?), 
> but then you wouldn't be able to have "private" scope.

When I say module scope, I'm referring to something along the lines that Arnaud 
and I were exploring a while back.  tldr, "cluster of files with a common 
namespace root, which can get loaded together."  It was mostly about 
performance, but did offer module-private as well, with some nice potential.  
At the moment it's stalled out on "there's nasty hard edge cases and we're not 
sure if it's worth it" concerns.

Concept brain dump here: 
https://github.com/Crell/php-rfcs/blob/master/modules/spec-brainstorm.md
Code exploration from Arnaud here: https://github.com/arnaud-lb/php-src/pull/10

Still well short of RFC state, of course, but provided for context.

> With nested/inner classes, for example, you can put a protected class 
> on a class or interface and access it only from those that use it, 
> regardless of what file or "module" (namespace?) they are in; the logic 
> can be encapsulated to where it is used, not where it is defined.
>
> interface Shipment {
>   protected class Items {}
>   public readonly class Destination {}
>   function deliver(self:>Destination $destination);
> }
>
> class InternationalShipment implements Shipment {
>   private function handle(Shipment:>Items $items) {}
>
>   public function deliver(Shipment:>Destination $destination) {}
> }

In this case, I am not seeing what the nesting gets you.  Making Destination a 
normal class doesn't hurt anything here, does it?

> However, you can use private nested/inner classes to encapsulate the 
> logic to where it is defined instead, like file-private would give you.
>
> The goal here isn't to only to reduce "namespace pollution" but also to 
> encapsulate related logic integrally connected to the outer class's 
> behavior. Since you've referenced Kotlin a number of times, I assume 
> you are familiar with the concept of nested classes there? This is 
> extremely similar.

My short foray into Kotlin did not include nested classes, so I cannot speak to 
them other than knowing they exist.

--Larry Garfield

Reply via email to