rfc:final_by_default_anonymous_classes

PHP RFC: Final by default anonymous classes

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.

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:

$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:

$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.

Make final anonymous classes final by default?
Real name Yes No
alcaeus (alcaeus)  
alec (alec)  
ashnazg (ashnazg)  
brzuchal (brzuchal)  
bukka (bukka)  
crell (crell)  
derick (derick)  
devnexen (devnexen)  
dharman (dharman)  
Fabien Potencier (fabpot)  
girgias (girgias)  
ilutov (ilutov)  
josh (josh)  
kalle (kalle)  
kguest (kguest)  
kocsismate (kocsismate)  
marandall (marandall)  
mbeccati (mbeccati)  
narf (narf)  
nicolasgrekas (nicolasgrekas)  
nikic (nikic)  
ocramius (ocramius)  
pierrick (pierrick)  
saki (saki)  
santiagolizardo (santiagolizardo)  
seld (seld)  
sergey (sergey)  
stas (stas)  
svpernova09 (svpernova09)  
theodorejb (theodorejb)  
thorstenr (thorstenr)  
timwolla (timwolla)  
trowski (trowski)  
villfa (villfa)  
weierophinney (weierophinney)  
Final result: 21 14
This poll has been closed.

2/3 required to accept.

Add an optional open keyword for anonymous classes?
Real name Yes No
alcaeus (alcaeus)  
ashnazg (ashnazg)  
brzuchal (brzuchal)  
bukka (bukka)  
crell (crell)  
devnexen (devnexen)  
dharman (dharman)  
Fabien Potencier (fabpot)  
girgias (girgias)  
ilutov (ilutov)  
kalle (kalle)  
kguest (kguest)  
kocsismate (kocsismate)  
marandall (marandall)  
narf (narf)  
nicolasgrekas (nicolasgrekas)  
nikic (nikic)  
ocramius (ocramius)  
pierrick (pierrick)  
saki (saki)  
santiagolizardo (santiagolizardo)  
sebastian (sebastian)  
sergey (sergey)  
svpernova09 (svpernova09)  
theodorejb (theodorejb)  
thorstenr (thorstenr)  
timwolla (timwolla)  
trowski (trowski)  
villfa (villfa)  
weierophinney (weierophinney)  
Final result: 0 30
This poll has been closed.

References

rfc/final_by_default_anonymous_classes.txt · Last modified: 2024/02/06 19:51 by ilutov