[php-src] PR #18207: RFC: Nested Classes
| From: | withinboredom | Date: | Mon, 31 Mar 2025 19:30:24 +0000 |
| Subject: | [php-src] PR #18207: RFC: Nested Classes | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-31268@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/18207
Author: withinboredom
This is a complete rewrite of the previous PR: #18069
There are a couple things going on here and can be broken up into two parts:
1. This makes namespaces into actual
zend_class_entry types. This is used by nested
classes for fast scope resolution and can be further used by a module implementation. These
namespaces cannot be instantiated but are simply used for scope resolution only, as every class now
has a "lexical scope" that is either a namespace or an outer class.
2. Adds support for nested classes using \ as a scope operator.
These two work together to allow nested types to be resolved correctly at compile time:
```php
namespace Foo\Bar;
class Inner {}
class Outer {
class Inner {}
class Middle {
class Inner {}
}
public function foo(Inner $inner): void {} // resolves to Foo\Bar\Outer\Inner
public function bar(\Foo\Bar\Inner $inner): void {} // FQN resolves to one in namespace
public function baz(Middle\Inner $inner): void {} // resolves to \Foo\Outer\Middle\Inner
}
```
This implementation works through the use of three members on the zend_class_entry
struct:
- required_scope: for requiring that the class is used within a specific scope.
- required_scope_absolute: when set to false, required_scope
checks can use instanceof, otherwise the required scope must match exactly; thus
false pertains to protected and true pertains to
private.
- lexical_scope: the class or namespace the class belongs to; used for visibility.
There are probably a number of things that can be improved in this PR, so feel free to suggest how
this can be improved.
I'd like to improve how namespaces are managed, for example (this PR inspired #18189 and will
likely benefit from what I learn there).
### Other attempts
A different approach would involve using name mangling instead; however, this created issues:
visibility and required scopes are subtly different from each other. This isn't as much an
issue with things like properties and methods, because they are always the same rules. Nested types
may be visible, but not be allowed to be used outside their scope:
```
class Outer {
private class Middle {
private class Inner {
public function foo(): self {}
}
}
private function foo(Middle\Inner $inner) {} // we can see Middle\Inner here
public function bar(Middle\Inner $inner) {} // we cannot expose it outside the outer scope
}
```
Outer::foo() can "see" Outer\Middle\Inner, but the inner types
cannot be exposed outside the outer class. These rules can possibly be implemented via name
mangling, but some benchmarks showed that even a deeply recursive tree of pointer equality is often
faster than even simple string operations.