A sealed type is one for which subclassing is restricted according to subclass names specified with the class’s declaration or to restrict to the same subclass namespace.
Sealed type is more like extended form of finality, where it is possible to list all class names allowed to extending from sealed class or permit only same namespace class to extend sealed class.
Purpose of sealing is that it restricts who can be a subtype. This is largely a declaration-site concern, where an API owner wants to defend the integrity of their API.
We specify that a class is sealed by applying the sealed
modifier to a class,
abstract class, or interface, with an optional permits
list:
sealed interface Node
permits A, B, C { ... }
In this explicit form, Node
may be extended only by the types enumerated in the permits
list. This means further extending a Node
class will raise a compile time error.
sealed interface Node
permits A { ... }
class Foo extends Node { ... } // Compile time error as Foo is not listed in permits clause
In case we not tend to list all allowed class names it is possible to seal a class
and allow extending in the same namespace without specifying permits
.
namespace MyTree;
sealed class Node { ... }
class Leaf extends Node { ... }
Which means all allowed extending classes must further be members of the same namespace.
Unless otherwise specified, abstract subtypes of sealed types are implicitly sealed, and concrete subtypes are implicitly final.
Anonymous subclasses of a sealed type are prohibited.
sealed
and permits
keywords become reserved word.
This is proposed for the next version of PHP, currently PHP 7.4.
The following additions will be made to expose the new flag via reflection:
- New constant
ReflectionClass::IS_SEALED
to expose the bit flag used for sealed classes - The return value of
ReflectionClass::getModifiers()
will have this bit set if the class being reflected is sealed Reflection::getModifierNames()
will include the string "sealed” if this bit is set- A new
ReflectionClass::isSealed()
method will allow directly checking if a class is locked - A new
ReflectionClass::getPermits()
method will include a list of allowed subclasses
None.
The RFC's compatibility with opcache is yet to be verified.
It merely adds a new ZEND_ACC_SEALED
flag so support shouldn't be a problem, however.
This does not affect methods or static properties.
As this is a language change, a 2/3 majority is required.
TBD
After the project is implemented, this section should contain
- the version(s) it was merged to
- a link to the git commit(s)
- a link to the PHP manual entry for the feature