rfc:locked-classes
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:locked-classes [2019/03/10 14:34] – created imsop | rfc:locked-classes [2019/03/11 11:03] – Fix formatting in reflection section nikic | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Locked Classes ====== | ====== PHP RFC: Locked Classes ====== | ||
- | * Version: | + | * Version: 1.0 |
- | * Date: 2019-03-06 | + | * Date: 2019-03-10 |
* Author: Rowan Collins [IMSoP], rowan.collins@gmail.com | * Author: Rowan Collins [IMSoP], rowan.collins@gmail.com | ||
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 10: | Line 10: | ||
Object properties in PHP are primarily defined in class definitions; | Object properties in PHP are primarily defined in class definitions; | ||
- | While setting properties which were not declared in a class definition, or unsetting properties which were declared, can be a useful tool, like many of PHP's dynamic features, it also makes certain mistakes easier. For instance, if a class defines a property '' | + | While setting properties which were not declared in a class definition, or unsetting properties which were declared, can be a useful tool, like many of PHP's dynamic features, it also makes certain mistakes easier. For instance, if a class defines a property '' |
Changing this behaviour for all objects would be a significant change to the language, with the potential to break a large amount of existing code. However, code written with no intention of using this dynamic behaviour would benefit from a way to switch it off. | Changing this behaviour for all objects would be a significant change to the language, with the potential to break a large amount of existing code. However, code written with no intention of using this dynamic behaviour would benefit from a way to switch it off. | ||
+ | |||
+ | While this can be achieved through strategic use of the '' | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 22: | Line 24: | ||
* Attempting to call '' | * Attempting to call '' | ||
- | This behaviour will interact | + | ==== Interaction |
- | | + | The existing " |
- | * If the class declares a magic '' | + | |
- | * If the class declares a magic '' | + | |
+ | * If the class declares a magic '' | ||
+ | * If the class declares a magic '' | ||
+ | |||
+ | ==== Interaction with Inheritance ==== | ||
+ | |||
+ | The " | ||
+ | |||
+ | The rationale is that a sub-class can add any number of public properties not listed in the parent definition, or add magic methods; it would therefore be inconsistent to ban it from allowing dynamic properties. | ||
+ | |||
+ | If the author of the class wants to prevent this, the combination "final locked class" can be used to prohibit any child classes, whether locked or not. | ||
+ | |||
+ | ===== Example ===== | ||
+ | |||
+ | For more examples, see the tests included in the draft implementation. | ||
+ | |||
+ | <code php> | ||
+ | locked class TestClass { | ||
+ | public $definedProp; | ||
+ | } | ||
+ | |||
+ | $t = new testClass(); | ||
+ | |||
+ | // Defined properties work as normal | ||
+ | $t-> | ||
+ | echo $t-> | ||
+ | unset($t-> | ||
+ | |||
+ | // Undefined properties may not be read or written | ||
+ | echo $t-> | ||
+ | $t-> | ||
+ | |||
+ | // Existing properties may not be unset | ||
+ | unset($t-> | ||
+ | </ | ||
===== Naming ===== | ===== Naming ===== | ||
- | Newer versions of ECMAScript / JavaScript have [[https:// | + | Newer versions of ECMAScript / JavaScript have [[https:// |
Since the proposed modifier applies to a class, not an instance, it would be confusing to use the keyword " | Since the proposed modifier applies to a class, not an instance, it would be confusing to use the keyword " | ||
+ | |||
+ | The name " | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | No internal classes, or classes in unmodified | + | The keyword " |
+ | |||
+ | No existing | ||
Since it is a new keyword, it will not be possible to " | Since it is a new keyword, it will not be possible to " | ||
Line 45: | Line 85: | ||
==== To Opcache ==== | ==== To Opcache ==== | ||
+ | |||
To be determined: are there any optimisations which interact with the behaviours being changed? | To be determined: are there any optimisations which interact with the behaviours being changed? | ||
==== To Reflection ==== | ==== To Reflection ==== | ||
- | * ReflectionClass:: | + | The following additions will be made to expose the new flag via reflection: |
- | * Changes | + | |
- | * ReflectionClass: | + | |
- | ===== Unaffected PHP Functionality ===== | + | * New constant ReflectionClass:: |
- | List existing areas/ | + | * The return value of ReflectionClass:: |
+ | * Reflection:: | ||
+ | * A new ReflectionClass:: | ||
- | This helps avoid any ambiguity, shows that you have thought deeply about the RFC's impact, and helps reduces mail list noise. | + | ===== Unaffected PHP Functionality ===== |
+ | Calling | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | TODO | + | Classes defined by extensions could be marked " |
===== Proposed Voting Choices ===== | ===== Proposed Voting Choices ===== | ||
Line 65: | Line 107: | ||
===== Patches and Tests ===== | ===== Patches and Tests ===== | ||
- | https:// | + | A pull request containing an initial implementation with basic tests is available on github: |
===== Implementation ===== | ===== Implementation ===== |
rfc/locked-classes.txt · Last modified: 2019/06/04 18:56 by imsop