rfc:deprecate_dynamic_properties
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
rfc:deprecate_dynamic_properties [2021/08/23 13:27] – created nikic | rfc:deprecate_dynamic_properties [2021/11/26 13:59] (current) – nikic | ||
---|---|---|---|
Line 2: | Line 2: | ||
* Date: 2021-08-23 | * Date: 2021-08-23 | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
- | * Status: | + | * Status: |
* Target Version: PHP 8.2 | * Target Version: PHP 8.2 | ||
- | * Implementation: | + | * Implementation: |
===== Introduction ===== | ===== Introduction ===== | ||
- | When writing to a property that has not been declared, PHP will silently create a dynamic property instead. In modern code, this is rarely done intentionally. This RFC proposes to deprecate and later remove the creation of dynamic properties | + | When writing to a property that has not been declared, PHP will silently create a dynamic property instead. In modern code, this is rarely done intentionally. This RFC proposes to deprecate and later remove the creation of dynamic properties, unless the class explicitly allows dynamic properties. |
<PHP> | <PHP> | ||
Line 22: | Line 22: | ||
// Oops, a typo: | // Oops, a typo: | ||
$user-> | $user-> | ||
- | // PHP <8.1: Silently creates dynamic $user-> | + | // PHP <= 8.1: Silently creates dynamic $user-> |
- | // PHP 8.2: Throws | + | // PHP 8.2: Raises |
- | // PHP 9.0: Throws Error exception. | + | // PHP 9.0: Throws Error exception. |
</ | </ | ||
" | " | ||
+ | |||
+ | ==== Motivation ==== | ||
+ | |||
+ | The motivation for this change is twofold: To prevent mistakes (due to typos or renames) in the common case, and to make intentional uses explicit. The core problem is that reading from a non-existing property issues a diagnostic that makes the issue immediately apparent, while writing to a non-existing property is entirely silent. PHP gives no indication whatsoever that the programmer has made a mistake. | ||
+ | |||
+ | A common counter-argument is that even if PHP itself does not detect the mistake, static analysis still can. While this is true to a degree, there are a number of problems: | ||
+ | |||
+ | - Static analysis in IDEs (probably the most widespread type of static analysis used in PHP) has to be conservative about diagnostics relating to dynamic properties. For example, PhpStorm treats creation of dynamic properties as only a weak warning (non-intrusive grey underline), because it cannot distinguish whether this dynamic property assignment is indeed a bug, or an intentional use. Treating dynamic property creation as a more severe error would result in false positives in cases where dynamic properties are used intentionally. The ''# | ||
+ | - Static analysis can only analyze assignments with a known object type and property name. This does not include any kind of dynamic assignments, | ||
+ | - On a more philosophical note, I believe that a programming language should be usable without external tooling. While IDE use is widespread among PHP programmers, | ||
+ | |||
+ | Finally, if classes using dynamic properties are explicitly declared, then we no longer need to reserve space for them on each object. This would reduce the size of all objects (that don't opt-in to dynamic properties) by 8 bytes. However, this is a fairly long-term benefit that will require additional technical work to realize. | ||
===== Proposal ===== | ===== Proposal ===== | ||
- | The creation of dynamic properties on classes that don' | + | The creation of dynamic properties on classes that aren' |
<PHP> | <PHP> | ||
Line 44: | Line 56: | ||
</ | </ | ||
- | Objects of type '' | + | Classes marked with '' |
<PHP> | <PHP> | ||
Line 52: | Line 64: | ||
$obj-> | $obj-> | ||
- | class myStdClass extends stdClass | + | # |
- | $obj2 = new myStdClass; | + | class Test {} |
+ | class Test2 extends Test {} | ||
// No deprecation warning | // No deprecation warning | ||
- | $obj2->bar = 1; | + | $obj = new Test; |
+ | $obj-> | ||
+ | |||
+ | // No deprecation warning | ||
+ | $obj = new Test2; | ||
+ | $obj->bar = 1; | ||
</ | </ | ||
- | |||
- | '' | ||
It should be noted that properties accessed through '' | It should be noted that properties accessed through '' | ||
Line 80: | Line 96: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | Removing support for dynamic properties constitutes a significant backwards compatibility break. While modern PHP code consistently declares used properties, this is not nececessarily | + | Removing support for dynamic properties constitutes a significant backwards compatibility break. While modern PHP code consistently declares used properties, this is not necessarily |
- | When encountered with a dynamic property deprecation warning, there are a number of things that can be done to avoid it. | + | The simplest and most common would be to simply add the property declaration: |
- | + | ||
- | The simplest and most common | + | |
<PHP> | <PHP> | ||
Line 96: | Line 110: | ||
</ | </ | ||
- | For classes that intentionally don't have a fixed set of properties, it's possible to either implement magic '' | + | For classes that intentionally don't have a fixed set of properties, it's possible to either implement magic '' |
- | + | ||
- | Using magic getters/ | + | |
In some cases it is desirable to associate information with objects that you do not own. Previously, it was possible to add a dynamic property for this purpose. Instead, a '' | In some cases it is desirable to associate information with objects that you do not own. Previously, it was possible to add a dynamic property for this purpose. Instead, a '' | ||
Line 133: | Line 145: | ||
A declared property that has been unset remains a declared property, and will not result in a dynamic property when it is reinitialized. | A declared property that has been unset remains a declared property, and will not result in a dynamic property when it is reinitialized. | ||
+ | |||
+ | ===== Discussion ===== | ||
+ | |||
+ | ==== Alternative opt-in to dynamic properties ==== | ||
+ | |||
+ | This RFC offers ''# | ||
+ | |||
+ | The difference between these approaches is in the end goal: ''# | ||
+ | |||
+ | While completely removing dynamic properties is a worthwhile end goal, we also need to acknowledge that dynamic properties have played an important historical role in PHP, and legacy codebases in particular may be making heavy use of them. While adding an attribute provides a straightforward upgrade path, extending '' | ||
+ | |||
+ | We may still wish to remove dynamic properties entirely at some later point. Having the ''# | ||
+ | |||
+ | ==== Opt-out of dynamic properties instead ==== | ||
+ | |||
+ | The [[rfc: | ||
+ | |||
+ | I don't believe that this is the right strategy, because in contemporary code, classes being " | ||
+ | |||
+ | An alternative that has been discussed in the context of the [[https:// | ||
+ | |||
+ | However, based on the discussion on the language evolution proposal, this would only delay the time where disallowed dynamic properties become the default and only behavior, as there was a strong consensus that diverging language behavior should not be maintained indefinitely. Dynamic properties would ultimately still get deprecated and removed. | ||
===== Vote ===== | ===== Vote ===== | ||
- | Yes/No. | + | Voting started 2021-11-12 and ended 2021-11-26. |
+ | |||
+ | <doodle title=" | ||
+ | | ||
+ | | ||
+ | </ |
rfc/deprecate_dynamic_properties.1629725237.txt.gz · Last modified: 2021/08/23 13:27 by nikic