# [RFC] New custom object serialization mechanism

- Thread: https://externals.io/message/103835
- Original archive: https://news-web.php.net/php.internals/103835
- Started by naitsirch@e.mail.de on 2019-01-24T15:06:12+00:00
- Messages: 2
- RFC: New custom object serialization mechanism (Implemented (PHP 7.4)) — https://externals.io/rfc/custom_object_serialization.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #103835 — naitsirch@e.mail.de — 2019-01-24T15:06:12+00:00

URL: https://externals.io/message/103835#103835

Sorry, forgot to CC the following mail to internals:

Am 24-Jan-2019 14:28:43 +0100 schrieb nikita.ppv@gmail.com:
> 
> 
> > […]
> 
> You mean if there are just those two methods, but not __unserialize() for example?
> In that case the array returned by __serialize() will be unserialized as object
> properties and __wakeup() will be called for finalization. In principle it's okay to
> use this combination, though it would be somewhat unusual.


Sorry, what I wanted to ask was, what happens if `__serialize()` and `__SLEEP()` are implemented? xD

Here's an example:

class A {
    private $foo = 'foo';
    private $bar = 'bar';

    function __serialize() {
        return ['foo' => $this->foo];
    }

    function __sleep() {
        return ['bar'];
    }
}

echo serialize(new A());

What will be the output of the above snippet?

> 
> > […]
> 
> I'd like that "serialization" appears in some form in the name, to make clear in what context this is used. Normalization is a fairly general term and could refer to any number of things.
> 
> Nikita

Yes, you are right. It is just, that my personal taste for function names is that they should describe what they do. New users may be irritated about the function name.
Maybe a more explicit name would be `__toSerializable`.

But I have no idea for the counterpart ^^

Best regards,

Christian
-------------------------------------------------------------------------------------------------
FreeMail powered by mail.de - MEHR SICHERHEIT, SERIOSITÄT UND KOMFORT

---

## #103837 — Rowan Collins — 2019-01-24T15:11:58+00:00

URL: https://externals.io/message/103835#103837 · In reply to: #103835

>
> Sorry, what I wanted to ask was, what happens if `__serialize()` and
> `__SLEEP()` are implemented? xD
>

This is covered in the RFC:

> If a class has both __sleep() and __serialize(), then the latter will be
preferred.

In other words, if both are included, only __serialize() will be run by PHP
>= 7.4 (but __sleep() will be called by PHP <= 7.3, providing an upgrade
path).

Regards,
-- 
Rowan Collins
[IMSoP]
