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:47] – nikic | rfc:deprecate_dynamic_properties [2021/08/25 14:12] – nikic | ||
---|---|---|---|
Line 8: | Line 8: | ||
===== 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. stdClass |
<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 136: | Line 136: | ||
==== Alternative opt-in to dynamic properties ==== | ==== Alternative opt-in to dynamic properties ==== | ||
- | This RFC offers '' | + | This RFC offers '' |
- | The reasoning behind the '' | + | The reasoning behind the '' |
+ | |||
+ | Another way to view this is that '' | ||
Using an interface or attribute instead would require the engine to continue supporting dynamic properties on arbitrary classes long term, rather than simply inheriting the behavior from a single class that implements the functionality. | Using an interface or attribute instead would require the engine to continue supporting dynamic properties on arbitrary classes long term, rather than simply inheriting the behavior from a single class that implements the functionality. | ||
+ | |||
+ | A trait based on '' | ||
+ | |||
+ | <PHP> | ||
+ | class DynamicProperties { | ||
+ | private array $dynamicProps = []; | ||
+ | public function & | ||
+ | public function __isset($name, | ||
+ | public function __set($name, | ||
+ | public function __unset($name) { unset($this-> | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | However, such a trait would not differ from simply implementing these methods in userland. Unlike '' | ||
==== Opt-out of dynamic properties instead ==== | ==== Opt-out of dynamic properties instead ==== | ||
Line 151: | Line 167: | ||
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. | 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. | ||
+ | |||
+ | ==== Internal impact ==== | ||
+ | |||
+ | Internal classes can already specify the '' | ||
+ | |||
+ | Once dynamic properties are disallowed, some larger changes should be made. Support for dynamic properties should be dropped from the virtual machine and default object handlers. Instead '' | ||
+ | |||
+ | Objects should no longer store a '' | ||
===== Vote ===== | ===== Vote ===== | ||
Yes/No. | Yes/No. |
rfc/deprecate_dynamic_properties.txt · Last modified: 2021/11/26 13:59 by nikic