# RFC: Final by default anonymous classes

- Status: Declined
- Authors: Daniil Gentili daniil@daniil.it
- Date: 2023-12-23
- Original: https://wiki.php.net/rfc/final_by_default_anonymous_classes
- Page: https://externals.io/rfc/final_by_default_anonymous_classes
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Make final anonymous classes final by default? (closed)

Yes: 21 · No: 14

Voters: alcaeus (No), alec (No), ashnazg (Yes), brzuchal (No), bukka (No), crell (Yes), derick (Yes), devnexen (No), dharman (No), fabpot (No), girgias (Yes), ilutov (Yes), josh (Yes), kalle (No), kguest (Yes), kocsismate (Yes), marandall (Yes), mbeccati (No), narf (No), nicolasgrekas (No), nikic (Yes), ocramius (Yes), pierrick (Yes), saki (No), santiagolizardo (Yes), seld (No), sergey (Yes), stas (Yes), svpernova09 (Yes), theodorejb (Yes), thorstenr (Yes), timwolla (Yes), trowski (Yes), villfa (No), weierophinney (Yes)

### Add an optional open keyword for anonymous classes? (closed)

Yes: 0 · No: 30

Voters: alcaeus (No), ashnazg (No), brzuchal (No), bukka (No), crell (No), devnexen (No), dharman (No), fabpot (No), girgias (No), ilutov (No), kalle (No), kguest (No), kocsismate (No), marandall (No), narf (No), nicolasgrekas (No), nikic (No), ocramius (No), pierrick (No), saki (No), santiagolizardo (No), sebastian (No), sergey (No), svpernova09 (No), theodorejb (No), thorstenr (No), timwolla (No), trowski (No), villfa (No), weierophinney (No)

## Discussions on the mailing list

- [Final anonymous classes](https://externals.io/message/121356.md) — started on 2023-10-17, 10 messages
- [\[RFC\] \[Discussion\] Final anonymous classes](https://externals.io/message/121685.md) — started on 2023-11-15, 6 messages
- [\[VOTE\] \[RFC\] Final anonymous classes](https://externals.io/message/121906.md) — started on 2023-12-03, 10 messages
- [\[RFC\] \[Discussion\] Final by default anonymous classes](https://externals.io/message/122026.md) — started on 2023-12-23, 1 messages
- [\[VOTE\] \[RFC\] Final-by-default anonymous classes](https://externals.io/message/122152.md) — started on 2024-01-15, 8 messages

---

## Introduction

This RFC proposes to make anonymous classes final by default.

## Proposal

This RFC proposes to make anonymous classes final by default.

The main reason for this change, in my view, is to allow some additional opcache optimizations, such as any JIT logic gated behind a check on ZEND\_ACC\_FINAL, i.e. [https://github.com/php/php-src/blob/master/ext/opcache/jit/zend\_jit\_trace.c#L4507](https://github.com/php/php-src/blob/master/ext/opcache/jit/zend_jit_trace.c#L4507).

Another reason is mentioned by Nikita in <https://externals.io/message/121906#121909>: “It makes very little sense to me that everyone needs to explicitly mark their anonymous classes as final just because there is a class\_alias loophole that could, in theory, have been used to extend anonymous classes in the past”.

Example, extending an anonymous class throws an error:

```php
$x = new class {};
class_alias($x::class, 'alias');
class aliasExtends extends alias {}
```

'' Fatal error: Class aliasExtends cannot extend final class class@anonymous in %s on line %d ''

Optionally, an `open` keyword inspired by Kotlin can be introduced to optionally make anonymous classes non-final.

Example, extending an open anonymous class does not throw an error:

```php
$x = new open class {};
class_alias($x::class, 'alias');
class aliasExtends extends alias {}
```

Rationale regarding `open`:

On one hand, looking at code like `new class {}`, you would assume that since the class apparently has no name, it should not be impossible to extend it, but on the other hand, there are valid usecases for extending even anonymous classes (comments in the PR (<https://github.com/php/php-src/pull/11126> referenced proxying), and I can also think of phpunit mocking: completely precluding the possibility of mocking a class that

1. Has a name (even if it's not immediately obvious)
2. Can be referenced to using its name (`class_exists`, `new ReflectionClass`, `new $clazz`, and yes, even `class_alias`)

seems a tad bit too restrictive...

On the other hand, I also really don't like non-final classes, in all of my projects, I use CS rules that force all classes to either be abstract or final, because unfortunately, I've had to work with a lot of code that very frequently violates encapsulation by using inheritance.

Still, there are some useful patterns, mainly regarding testing and mocking, for example I use <https://github.com/dg/bypass-finals> as a dev dependency to make all final classes non-final at runtime to allow mocking in phpunit, but it works by installing a custom default stream contexts that intercepts requires, tokenizes files and removes final keywords from classes; this approach would break for anonymous classes if they were rendered final by default without an option to make them non-final.

Thus, I also propose the additional of an optional `open` keyword that can be used to make anonymous classes non-final.

The implementation PR will be provided by me in case of acceptance.

## Backward Incompatible Changes

Anonymous classes will be rendered final by default.

## Proposed PHP Version(s)

Next PHP 8.4

## RFC Impact

See Backward Incompatible Changes.

## Proposed Voting Choices

Voting started on January 15th, 2024 and runs until January 30th, 2024, 00:00 UTC.

2/3 required to accept.

**Vote: Make final anonymous classes final by default?** — Yes: 21 · No: 14 (closed)

2/3 required to accept.

**Vote: Add an optional open keyword for anonymous classes?** — Yes: 0 · No: 30 (closed)

## References

- Internals discussion: <https://externals.io/message/122026>, <https://externals.io/message/121356>, <https://externals.io/message/121685>, <https://externals.io/message/122026>
- Previous optionally final anonymous classes RFC: [https://wiki.php.net/rfc/final\_anonymous\_classes](https://externals.io/rfc/final_anonymous_classes)
