rfc:sealed_classes

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
rfc:sealed_classes [2022/03/02 23:57] azjezzrfc:sealed_classes [2022/03/14 16:49] – typo azjezz
Line 169: Line 169:
 1. shared functionality 1. shared functionality
  
-Sealed classes feature allow you to implement functionalities in the is common between sub-types, such as:+Sealed classes feature allow you to implement functionalities in the parent class, such as:
  
 <code> <code>
Line 247: Line 247:
  
 The output of `consumer` could be either `"A"`, `"B"`, or `"C"`, as `A` is a non-abstract class, it is possible to do `consumer(new A())`. The output of `consumer` could be either `"A"`, `"B"`, or `"C"`, as `A` is a non-abstract class, it is possible to do `consumer(new A())`.
- 
  
 ===== Syntax ===== ===== Syntax =====
Line 285: Line 284:
 trait Corge for Grault, Garply {} trait Corge for Grault, Garply {}
 </code> </code>
 +
 +
 +===== FAQ's =====
 +
 +== Wouldn't a sealed class without permits clauses be considered final? ==
 +
 +No, a sealed class will always have a `permits` clauses, if a sealed class is defined without a `permits` clauses, it's considered
 +a compile error.
 +
 +
 +== Would PHP check if permitted classes exists when loading a sealed class? ==
 +
 +No, when loading a sealed class, PHP would treat just like any other class, and store the permitted types list to check against
 +later when another type tries to inherit from it.
 +
 +== What if the permitted types don't actually extend the sealed type ==
 +
 +Example:
 +
 +<code php>
 +sealed interface A permits B {}
 +
 +class B {}
 +</code>
 +
 +This code would not produce any errors, as another type ( e.g: `C` ) could exist, in which it inherits from both `B`, and `A`, therefor, an instance of `A&B` could still exist.
 +
 +== What if the permitted types don't actually extend the sealed type, and are final ==
 +
 +Example:
 +
 +<code php>
 +sealed interface A permits B {}
 +
 +final class B {}
 +</code>
 +
 +In this case, we would end up with an interface 'A', but with no possible instance of it, however, due to the behavior stated above of only checking permitted types on inheritance and not when loading the sealed type, this is allowed, and is considered a small inconvenience.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 334: Line 371:
 ===== Changelog ===== ===== Changelog =====
  
-1.1: added comparison to composite types+ 
 +  * 1.1: added comparison to composite types
 +  * 1.2: added FAQ's section.
  
rfc/sealed_classes.txt · Last modified: 2022/04/01 08:21 by azjezz