Skip to content

Conversation

@jrfnl
Copy link
Member

@jrfnl jrfnl commented Aug 20, 2019

PHP 7.4/NewMagicMethods: handle new __(un)serialize()

A new mechanism for custom object serialization has been added, which uses two new magic methods:

```php
// Returns array containing all the necessary state of the object.
public function __serialize(): array;

// Restores the object state from the given data array.
public function __unserialize(array $data): void;
```

The new serialization mechanism supersedes the Serializable interface, which will be deprecated in the future.

Refs:

Includes unit tests.

PHP 7.4/NonStaticMagicMethods: handle new __(un)serialize()

From the RFC:

Creating objects in __unserialize()

Some people have expressed a desire to make __unserialize() a static method which creates and returns the unserialized object (rather than first constructing the object and then calling __unserialize() to initialize it).

This would allow an even greater degree of control over the serialization mechanism, for example it would allow to return an already existing object from __unserialize().

However, allowing this would once again require immediately calling __unserialize() functions (interleaved with unserialization) to make the object available for backreferences, which would reintroduce some of the problems that Serializable suffers from. As such, this will not be supported.

Refs:

Includes unit tests.

Related to #808

jrfnl added 2 commits August 20, 2019 08:53
> A new mechanism for custom object serialization has been added, which uses two new magic methods:
>
>     ```php
>     // Returns array containing all the necessary state of the object.
>     public function __serialize(): array;
>
>     // Restores the object state from the given data array.
>     public function __unserialize(array $data): void;
>     ```
>
> The new serialization mechanism supersedes the Serializable interface, which will be deprecated in the future.

Refs:
* https://wiki.php.net/rfc/custom_object_serialization
* https://github.com/php/php-src/blob/3775d47eee38f3b34f800a0b23f840ec7a94e4c7/UPGRADING#L305-L315
* php/php-src#3761
* php/php-src@d373c11

Includes unit tests.

Related to 808
From the RFC:

> **Creating objects in __unserialize()**
>
> Some people have expressed a desire to make __unserialize() a static method which creates and returns the unserialized object (rather than first constructing the object and then calling __unserialize() to initialize it).
>
> This would allow an even greater degree of control over the serialization mechanism, for example it would allow to return an already existing object from __unserialize().
>
> However, allowing this would once again require immediately calling __unserialize() functions (interleaved with unserialization) to make the object available for backreferences, which would reintroduce some of the problems that Serializable suffers from. As such, this will not be supported.

Refs:
* https://wiki.php.net/rfc/custom_object_serialization
* https://github.com/php/php-src/blob/3775d47eee38f3b34f800a0b23f840ec7a94e4c7/UPGRADING#L305-L315
* php/php-src#3761
* php/php-src@d373c11

Includes unit tests.

Related to 808
@jrfnl jrfnl added this to the 9.3.0 milestone Aug 20, 2019
@jrfnl jrfnl requested a review from wimg August 20, 2019 06:59
@jrfnl jrfnl added the PR: quick merge PR only contains relatively simple changes label Aug 20, 2019
@wimg wimg merged commit 6598d41 into master Aug 26, 2019
@delete-merged-branch delete-merged-branch bot deleted the php-7.4/custom-object-serialization branch August 26, 2019 16:09
@jrfnl jrfnl removed PR: ready for review PR: quick merge PR only contains relatively simple changes labels Sep 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants