[php-src] Issue #12754: Anonymous class without new
| From: | mvorisek | Date: | Thu, 23 Nov 2023 01:18:51 +0000 |
| Subject: | [php-src] Issue #12754: Anonymous class without new | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245877@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12754
Author: mvorisek
### Description
Currently, anonymous classes must be immediatelly instanced.
I would be happy if they can declared only and the class name returned (to be assigned or passed
directly).
# Current syntax
```
class Control {}
$controlClass = get_class(new class() extends Control {
private static bool $firstCreate = true;
public function __construct($defaults = [])
{
if (self::$firstCreate) {
self::$firstCreate = false;
return;
}
throw new Exception('x');
}
});
```
# Expected syntax
```
class Control {}
$controlClass = class() extends Control {
public function __construct($defaults = [])
{
throw new Exception('x');
}
};
```
Anonymous classes are used a lot for example in tests and the shorter syntax can make the code
simpler.
As
class is a reserved word, it should be feasible and not ambiguous even with some
possible future language features, as the only possible tokens sequence would be class
{, class extends or class(.