Re: RFC: short and inner classes
| From: | Tim Düsterhus | Date: | Thu, 06 Mar 2025 22:31:35 +0000 |
| Subject: | Re: RFC: short and inner classes | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-126612@lists.php.net to get a copy of this message | ||
Hi
On 3/6/25 23:05, Rob Landers wrote:
Okay, does calling the closure work and correctly call theYou end up with: object(Closure)#1 (1) { ["function"]=> string(20) "Outer::Inner::method" }Closure::fromCallable('Outer::Inner::method');
method on the inner class? The question was intended to make sure that the implementation for callables uses the correct :: to split. Here's another one that might be interesting:
Closure::fromCallable(["Outer::Inner", "method"]);
Closure::fromCallable(["Outer", "Inner::method"]);
Okay, so this behaves as a constant containing the class name. I assume it's with the full namespace if the outer class is namespaced? I'm not sure if I want this to work like this (i.e. whether this should be an error).This returns: string(12) "Outer::Inner"constant('Outer::Inner');
It's consistent withThis does nothing (but resolves to "Outer::Inner")$inner = 'Inner'; Outer::{$inner};
constant() and that's good.
I don't think the RFC explains “resolution” at all. That's why I'm asking with those specific “edge-casey” examples, so that the RFC explicitly spells out the behavior. This is not something that should be “implementation defined”, but something where an explicit design decision has been made. I also don't understand why… and any other meta-programming functionality working on class constants or static methods. Also, what will happen for:This is a really good one. If for no other reason than I did a really poor job of explaining resolution(?) in the RFC.class P { class Inner { } }class C extends P { const Inner = 'x'; }(and vice versa)P::Innerbelongs toP, not toC, so you can donew C::Inner()and it will resolve toP::Inner():
new C::Inner() (w|sh)ould resolve to P::Inner(). I think my expectation of the code snippet above would be that it is an error.
Likewise, LSP being ignored for inner classes raises an interesting question about the behavior of:
class P {
class Inner {
public function __construct(public string $foo) { }
}
public static function create() {
return new static::Inner('x');
}
}
class C extends P {
class Inner {
public function __construct(public int $bar) { }
}
}
What happens if I call C::create()? This should also be specified in the RFC (and tested with a .phpt test).
As with other static things in PHP, you can do some really strange things like this. This is similar to how you can redefine static constants in subclasses.We should remove the number of strange things, not add to them. Best regards Tim Düsterhus