Skip to content

Implement new custom object serialization mechanism #3761

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

Closed
wants to merge 1 commit into from

Conversation

nikic
Copy link
Member

@nikic nikic commented Jan 23, 2019

@nikic nikic added the RFC label Jan 23, 2019
@nikic
Copy link
Member Author

nikic commented Mar 22, 2019

Merged as d373c11.

@nikic nikic closed this Mar 22, 2019
@nicolas-grekas
Copy link
Contributor

For reference, I opened https://bugs.php.net/77866: internal Serializable classes should implement the new __(un)serialize methods, isn't it?

@yitam
Copy link

yitam commented Jun 22, 2019

Not sure if this is the right place to ask about __wakeup() and __sleep() being removed from pdo pdo_dbh.c and pdo_stmt.c. In the aforementioned RFC it said "no particular pressing need to phase out __sleep() and __wakeup()". Please advice.

jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Aug 20, 2019
> 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
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Aug 20, 2019
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
@caiofior
Copy link

caiofior commented Jun 6, 2024

I had an issue in a project made with Zend Framework 2.2 using session storage related to the class ArrayObject

namespace Zend\Stdlib;

use ArrayAccess;
use Countable;
use IteratorAggregate;
use Serializable;

/**
 * Custom framework ArrayObject implementation
 *
 * Extends version-specific "abstract" implementation.
 */
class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable
{
    public function __serialize() {return $this->serialize();}
    public function __unserialize($data) {$this->unserialize($data);}

...

    /**
     * Unserialize an ArrayObject
     *
     * @param  string $data
     * @return void
     */
    public function unserialize($data)
    {
        $ar                        = unserialize($data);

In php greather than 7.3 the was a fatal error because the method _serialize must return an array.

The solution is a little involuted

namespace Zend\Stdlib;

use ArrayAccess;
use Countable;
use IteratorAggregate;
use Serializable;

/**
 * Custom framework ArrayObject implementation
 *
 * Extends version-specific "abstract" implementation.
 */
class ArrayObject implements IteratorAggregate, ArrayAccess, Serializable, Countable
{
    public function __serialize() {return (array)$this->serialize();}
    public function __unserialize($data) {$this->unserialize($data);}

...

    /**
     * Unserialize an ArrayObject
     *
     * @param  string $data
     * @return void
     */
    public function unserialize($data)
    {
        if (is_array($data)) {
            $data = reset($data);
        }
        $ar                        = unserialize($data);

Why __serialize() must return an array?
I suppose it returns a string.

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

Successfully merging this pull request may close these issues.

4 participants