Both sides previous revisionPrevious revisionNext revision | Previous revision |
rfc:final_by_default_anonymous_classes [2024/01/15 10:26] – danog | rfc:final_by_default_anonymous_classes [2024/02/06 19:51] (current) – Move to declined ilutov |
---|
* Date: 2023-12-23 | * Date: 2023-12-23 |
* Author: Daniil Gentili <daniil@daniil.it> | * Author: Daniil Gentili <daniil@daniil.it> |
* Status: Voting | * Status: Declined |
| |
===== Introduction ===== | ===== Introduction ===== |
This RFC proposes to make anonymous classes final by default. | This RFC proposes to make anonymous classes final by default. |
| |
This should also 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. | 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. |
| |
| 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: | Example, extending an anonymous class throws an error: |
| |
| |
Optionally, an ''open'' keyword can be introduced to optionally make anonymous classes non-final. | 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: | Example, extending an open anonymous class does not throw an error: |
</code> | </code> |
| |
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), I can think of phpunit mocking to a much, much lesser extent given that you should realistically (hopefully) never have to mock an anonymous class that does not already implement an interface), and completely precluding the possibility of extending a class that | 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 |
| |
- Has a name (even if it's not immediately obvious) | - Has a name (even if it's not immediately obvious) |
2/3 required to accept. | 2/3 required to accept. |
| |
<doodle title="Make final anonymous classes final by default?" auth="danog" voteType="single" closed="false" closeon="2024-01-30T00:00:00Z"> | <doodle title="Make final anonymous classes final by default?" auth="danog" voteType="single" closed="true" closeon="2024-01-30T00:00:00Z"> |
* Yes | * Yes |
* No | * No |
2/3 required to accept. | 2/3 required to accept. |
| |
<doodle title="Add an optional open keyword for anonymous classes?" auth="danog" voteType="single" closed="false" closeon="2024-01-30T00:00:00Z"> | <doodle title="Add an optional open keyword for anonymous classes?" auth="danog" voteType="single" closed="true" closeon="2024-01-30T00:00:00Z"> |
* Yes | * Yes |
* No | * No |