rfc:deprecate_dynamic_properties
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:deprecate_dynamic_properties [2021/08/25 09:13] – nikic | rfc:deprecate_dynamic_properties [2021/11/10 14:25] – 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 | + | // PHP 8.2: Raises |
- | // PHP | + | // PHP 9.0: Throws Error exception. |
</ | </ | ||
Line 31: | Line 31: | ||
===== 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 44: | ||
</ | </ | ||
- | Objects of type '' | + | Classes marked with '' |
<PHP> | <PHP> | ||
Line 52: | Line 52: | ||
$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->bar = 1; |
- | '' | + | // No deprecation warning |
+ | $obj = new Test2; | ||
+ | $obj-> | ||
+ | </ | ||
It should be noted that properties accessed through '' | It should be noted that properties accessed through '' | ||
Line 94: | Line 98: | ||
</ | </ | ||
- | 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 131: | Line 133: | ||
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. | Yes/No. |
rfc/deprecate_dynamic_properties.txt · Last modified: 2021/11/26 13:59 by nikic