[php-src] Issue #12336: Intuitive way to convert constructor to closure
| From: | mindplay-dk | Date: | Sun, 01 Oct 2023 08:54:30 +0000 |
| Subject: | [php-src] Issue #12336: Intuitive way to convert constructor to closure | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245471@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12336
Author: mindplay-dk
### Description
I wish one of these ways worked:
```php
<?php
class Foo
{
public function __construct(public string $lol) {}
}
$class = new ReflectionClass(Foo::class);
// method 1:
$ctor = $class->getConstructor()->getClosure(); // ReflectionMethod::getClosure(): Argument #1
($object) cannot be null for non-static methods
$classname = Foo::class;
// method 2:
$ctor = new $classname(...); // Fatal error: Cannot create Closure for new expression
```
I'd expect both approaches to work - a constructor is conceptually just a static function that
generates a new instance, although the reflection facility doesn't treat it or report it as
such.
The second approach at least ought to work - for consistency with that feature.
Even
new Foo(...) doesn't work - and judging by the error message, someone
predicted what I'm trying to do, but it just isn't supported... yet? is this feature
planned?
(I know there are ways to do it, using reflection, so you don't need to explain that - just
wondering why this feature isn't available, as it would seem natural to expect this to work.)