Re: [RFC] Deprecate dynamic properties
| From: | Andreas Leathley | Date: | Wed, 25 Aug 2021 12:35:59 +0000 |
| Subject: | Re: [RFC] Deprecate dynamic properties | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-115809@lists.php.net to get a copy of this message | ||
On 25.08.21 12:45, Rowan Tommins wrote:
* Adding a parent to an existing class isn't always possible, if it already inherits from something else. Perhaps the behaviour could also be available as a trait, which defined stub __get and __set methods, allowing for the replacement of the internal implementation as you've described? Couldn't this be easily done in userland with the ArrayLikeObject (anexample in the RFC) or something similar, just done as a trait? Maybe having an example trait implementation that basically "imitates" stdClass in terms of dynamic properties would be helpful to show in the RFC, to highlight a possible resolution path for legacy code with this problem. This could be an example implementation: trait DynamicPropertiesTrait {
private array $dynamicProperties = [];
public function &__get(string $name): mixed { return
$this->dynamicProperties[$name]; }
public function __isset(string $name): bool { return
isset($this->dynamicProperties[$name]); }
public function __set(string $name, mixed $value): void {
$this->dynamicProperties[$name] = $value; }
public function __unset(string $name): void {
unset($this->dynamicProperties[$name]); }
}