Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support php 7.4's __unserialize to prepare for PHP 9.0 removing Serializable #179

Open
TysonAndre opened this issue Sep 26, 2021 · 0 comments

Comments

@TysonAndre
Copy link

Noticed when running pecl install ds then checking by evaluating php snippets to see if php-ds allowed dynamic properties (ce_flags ZEND_ACC_NO_DYNAMIC_PROPERTIES - apparently, it allows them) and whether it bothers serializing dynamic properties (it doesn't), since this type of behavior usually isn't documented on php.net

php > $a = new Ds\Vector();
php > $a[] = 'value';
php > $a->dynamicProp = 'not serialized';
php > echo serialize($a);
C:9:"Ds\Vector":12:{s:5:"value";}

(I'm not requesting supporting serializing dynamic properties or changes to property behavior, I wanted to know if php-ds made a design choice to reject dynamic properties, especially with https://wiki.php.net/rfc/deprecate_dynamic_properties recently being proposed)

https://wiki.php.net/rfc/phase_out_serializable mentions that Serializable and the C: serialization format will be phased out in php 9.0

In PHP 9.0 the Serializable interface will be removed and unserialize() will reject payloads using the C serialization format. Code needing to support both PHP < 7.4 and PHP >= 9.0 may polyfill the Serializable interface, though it will have no effect on serialization.

For users that are migrating from an older php version (and php-ds installation) to php 9.0 in the future, it would be useful to support __unserialize in one release (and switch the way structures are serialized to __serialize in a later release). When implementing __unserialize, one edge case that needs to be handled is checking if the data is already unserialized.

  • __unserialize requires php 7.4+ to work. In older versions, the array data being unserialized is added as object properties instead. __wakeup can be used inconveniently in php versions prior to 7.4 to some extent to try to convert those dynamic properties back to a well-formed collection, but I've never seen anything that does that, so I'm not certain of the difficulty
  • E.g. data structures might be serialized and saved in/loaded from memcache/redis, and users may run both php 8.1 and 9.0 at the same time, and making sure that php 8.1 could read the data written by 9.0 would be useful

(I'm one of the maintainers of igbinary, I should be able to answer questions about serialization/unserialization in general. Note that igbinary probably does not work with php-ds's use of Serializable design for edge cases such as object reuse, cyclic data structures, etc)

php-src/ext/spl/ or other libraries can be used as a reference for how to implement an __unserialize method in a datastructure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant