On 15.3.2025 00:21:32, Rowan Tommins [IMSoP] wrote:
On 05/03/2025 23:11, Rob Landers wrote:
I'd like to introduce my RFC for discussion:
https://wiki.php.net/rfc/short-and-inner-classes
As a user, I find the concept of inner classes quite confusing.
However, I was looking at some code earlier and thought an "inner
enum" would be useful, to replace various "MODE_FOO" type class
constants - but I see those are left to future scope. :(
Yeah, that's quite disappointing. I expected this to simply apply to all
types of "class" until I saw the explicit exclusion.
I'm aware they exist in a lot of other languages, though, so I thought
I'd look around at how the proposed version compares. It seems there's
quite a zoo out there...
One common theme I do note is that all four of the pages I found are
titled "nested classes" or "nested types"; in some cases, "inner
class"/"inner type" has a specific meaning, which I don't think
matches the RFC's proposal.
C# -
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/nested-types
I think this is most similar to what you're proposing. Interestingly,
the nested class defaults to being private, i.e. only usable within
the parent class.
The default visibility in c# is private, for everything. The RFC is
consistent with the default visibility in PHP: public. c# is consistent
with the default visibility for c#.
Classes, structs and interfaces can all be nested inside each other.
Java - https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
There are two types of nested class (plus some more related concepts
which seem less relevant)
- "Static nested classes" - static in the sense that they act like a
static member of the surrounding class; appear to be *only* about
namespacing and visibility of the class, with no special access to the
surrounding scope
- "Inner classes" - associated not just with a parent class, but a
parent *instance*, so have to be created with the syntax
"ParentClassName.new InnerClassName" or "this.new InnerClassName"; are
not allowed to have static members
inner (instance) classes in Java are quite a weird beast. I don't think
it fits PHP paradigm nicely.
Bob