rfc:final_anonymous_classes

This is an old revision of the document!


PHP RFC: Final anonymous classes

  • Date: 2023-11-15
  • Author: Daniil Gentili daniil@daniil.it
  • Status: Under Discussion

Introduction

This RFC proposes to add support for final anonymous classes, or make all anonymous classes final by default, with or without the possibility to make them non-final.

Proposal

This RFC proposes to either add support for final anonymous classes, or make all anonymous classes final by default, with or without the possibility to make them non-final.

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.

Personally, I would have instead preferred the much cleaner approach of making all anonymous classes final by default, (preferrably) without offering the option to make them non-final.

However, I understand that this might be a little bit too restrictive for something that may have some valid usecases, even if extending anonymous classes currently requires some hack-ish workarounds with class_alias.

Thus, this RFC includes three mutually exclusive proposals:

- Add support for final anonymous classes (new final class {} syntax, no breaking changes)

- OR Make all anonymous classes final by default, without the option to make them final (breaking change)

- OR Make all anonymous classes final by default, provide an optional open keyword to make them non-final (like in Kotlin, new open class {}, no breaking changes).

As an extra proposal related to the last two options, possibly requiring a separate RFC, in the last two cases, it might be a good idea to also disallow the use of class_alias altogether for final anonymous classes (suggested by nikolas-grekas in https://github.com/php/php-src/pull/11126#issuecomment-1522042841).

Backward Incompatible Changes

In case proposal 2 is accepted, it will break existing code which extends from anonymous classes using class_alias.

In case proposal 2 is accepted and class_alias is disallowed for final anonymous classes, it will break existing code that uses class_alias to give a name to anonymous classes, not necessarily to extend them.

Proposed PHP Version(s)

Next PHP 8.4

RFC Impact

See Backward Incompatible Changes.

Future Scope

As an extra proposal related to the last two options, possibly requiring a separate RFC, in the last two cases, it might be a good idea to also disallow the use of class_alias altogether for final anonymous classes (suggested by nikolas-grekas in https://github.com/php/php-src/pull/11126#issuecomment-1522042841).

Proposed Voting Choices

2/3 required to accept.

Add support for final anonymous classes?
Real name Yes No
alcaeus  
alec  
ashnazg  
brzuchal  
crell  
cschneid  
derick  
devnexen  
dharman  
galvao  
geekcom  
heiglandreas  
kalle  
kguest  
levim  
mbeccati  
nicolasgrekas  
nielsdos  
nikic  
ocramius  
ramsey  
sebastian  
stas  
weierophinney  
Final result: 8 16
This poll has been closed.

2/3 required to accept.

Make all anonymous classes final by default, without the option to make them non-final?
Real name Yes No
Final result: 0 0
This poll has been closed.

2/3 required to accept.

Make all anonymous classes final by default, with the option to make them non-final with an ''open'' keyword?
Real name Yes No
Final result: 0 0
This poll has been closed.

Patches and Tests

Final anonymous classes implementation: https://github.com/php/php-src/pull/11126

References

rfc/final_anonymous_classes.1700069810.txt.gz · Last modified: (external edit)