Skip to content

PHP 8.2 would deprecate dynamic properties by default: Consider #[\AllowDynamicProperties] #388

@TysonAndre

Description

@TysonAndre

NOTE: In my own use case (https://github.com/phan/phan), I use tolerant-php-parser as part of a polyfill that I convert to ast\Node and I end up associating dynamic information with ast\Node instances, so I don't personally need this (they all work fine in php 8.2), but other projects might.
I'm bringing this up so that it can be considered before projects are affected

  • E.g. for caching inferred types, or caching expensive operations such as whether a given node has been processed in a given way, etc

PHP 8.2 deprecates dynamic properties by default and PHP 9.0 will remove support by default without the AllowDynamicProperties annotation. https://wiki.php.net/rfc/deprecate_dynamic_properties

  • Projects using tolerant-php-parser can use WeakMap as a substitute for dynamic properties on Microsoft\PhpParser\Node (though it had worse performance until the latest php minor releases). For php 7.4 and older, https://github.com/TysonAndre/pecl-weakreference_bc is an option I'd created (expecting projects to benefit from this for 8.2 in general) but has low adoption. More conveniently than using a PECL, hybrid approaches such as using dynamic properties/SplObjectStorage in older versions and WeakMaps in newer versions might be possible.

The approaches used in similar libraries:


Options:

  1. Add #[\AllowDynamicProperties] to classes in the current release line, so that php 8.1 and 8.2 will behave the same way, and applications can stay as is

    In 8.x, the memory usage is the same with/without this property

  2. Do nothing, let projects implement alternate solutions when they fix 8.2 deprecations or support 9.0

  3. Add attributes array nodes so that projects can switch to that (increase memory slightly per node by 16 bytes for a zval when empty)


This is out of scope of what I'd prefer in the 0.1.2 release

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions