rfc:final_class_const

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
rfc:final_class_const [2021/05/04 15:06] kocsismaterfc:final_class_const [2021/05/04 15:12] kocsismate
Line 11: Line 11:
 First of all, the engine can't optimize class constant references when late static binding is involved, so it has to pessimistically assume that ''FOO'' is overridden in case of ''static::FOO'' or ''$this::FOO'' invocations. First of all, the engine can't optimize class constant references when late static binding is involved, so it has to pessimistically assume that ''FOO'' is overridden in case of ''static::FOO'' or ''$this::FOO'' invocations.
  
-What's more important is that class constants are not guaranteed to stay constant. Even if one always references them by using ''self::'', doing so won't prevent a child class from changing their value or even their type. Although constants not being constants is usually considered only as a theoretical problem, being able to add a ''final'' modifier would make the intention explicit that child classes shouldn't try to override them (e.g. because the parent .+What's more important is that class constants are not guaranteed to stay constant. Even if the declaring class always references them by using ''self::'', doing so won't prevent a child class from changing their value or even their type, unless the parent is a final class. Although constants not being constants is usually considered only as a theoretical problem, being able to add a ''final'' modifier would make the intention explicit that child classes shouldn't try to override them (e.g. because the parent doesn't use late static binding).
  
-An interesting fact is that interface constants are already ''final'':+A related interesting fact is that interface constants are already ''final'':
  
 <code php> <code php>
Line 29: Line 29:
 </code> </code>
  
-This leads to an interesting inconsistency. By introducing an intermediate class, overriding still becomes possible:+This leads to a weird inconsistency. By introducing an intermediate class, overriding still becomes possible:
  
 <code php> <code php>
Line 88: Line 88:
 ===== Reflection ===== ===== Reflection =====
  
-A ''ReflectionClassConstant::isFinal()''method is added in order to be able to retrieve if a constant is final.+A ''ReflectionClassConstant::isFinal()'' method is added in order to be able to retrieve if a constant is final.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/final_class_const.txt · Last modified: 2021/07/06 20:52 by kocsismate