Re: Re: RFC: Nested Classes (was: short and inner classes)

From: Date: Tue, 22 Apr 2025 17:30:00 +0000
Subject: Re: Re: RFC: Nested Classes (was: short and inner classes)
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message


On Tue, Apr 22, 2025, at 19:22, Levi Morrison wrote:
> On Sun, Apr 20, 2025 at 7:46 AM Rob Landers <[email protected]> wrote:
> >
> > On Mon, Mar 31, 2025, at 21:45, Rob Landers wrote:
> >
> > Hello internals,
> >
> > I have significantly revamped the RFC (again). Key changes to the RFC:
> >
> > 1. More (realistic) examples,
> > 2. Since enums are basically specialized classes, they are allowed to be nested as well
> > (hat tip to Reddit),
> > 3. Using backslash as the class separator,
> > 4. Proper scoping (and shadowing),
> > 5. Nesting is allowed in interfaces and enums as well as classes; but not traits,
> > 6. (Hopefully) Clearer wording,
> > 7. Nesting in traits, or nested traits, are future scope,
> > 8. Nested interfaces are future scope too.
> >
> > Some benefits of using \ as a separator:
> >
> > - a simple name can refer to nested classes:
> >
> > Scope resolution was expanded to treat inner classes within the same class as “in
> > scope.” This provides a more natural usage:
> >
> > class Outer {
> >   class Inner {}
> >   public function foo(Inner $inner) {}
> > }
> >
> > - Standard “use” statements can alias them:
> >
> > use Outer\Inner as Inner;
> >
> > But it also has some draw backs:
> >
> > - The engine doesn’t know that Outer\Inner is a nested class and autoloaders will have
> > to account for that. It will only ask for Outer\Inner.
> >
> > - You cannot simply refer to parent:>Inner, you have to explicitly ask for the parent
> > by name: SomeParentClass\Inner.
> >
> > A draft implementation (which is more of a proof-of-concept) is available on GitHub.
> >
> >
> > Hello internals,
> >
> > As it seems that discussion has mostly died down, I'd like to put this towards a vote
> > starting on May 1, 2025.
> >
> > — Rob
> 
> I intend to vote no. Fundamentally, this proposal adds a form of
> "packaging" which only affects classes. There's no packaging for
> constants or functions, unless you put them onto a class to make them
> class constants and static methods. This is weird to me. I would
> prefer that we work out something more general.

Thank you for your feedback! It very much isn't packaging, this is closer to "friend"
classes in C++ or nested classes in other languages (C#, Kotlin, Swift, Java, etc). I do intend to
focus on packaging (in-general) in the near future though.

> 
> I am also worried that naming collisions are possible. Something like this:
> 
> ```php
> namespace A {
>      class B {}
> }
> 
> namespace {
>     class A {
>         class B {}
>     }
> }
> ```
> 
> Where A\B refers to two possible things. I don't like this, and to
> my knowledge, this type of confusion has not been possible in the
> past. Of course, feel free to point it out if so. Note that methods
> and properties of the same name are disambiguated by syntax, where
> here the syntax is the same.
> 

I should update the RFC to include this case, thanks for pointing this out. This would cause the
ole' "Cannot redeclare class A\B" error. There is not any ambiguity here.

— Rob


Thread (102 messages)

« previous php.internals (#127168) next »