Doc #79352 [Opn]: ArrayObject::serialize and ArrayObject::unserialize are no longer overrideable
| From: | nikic@php.net | Date: | Sat, 07 Mar 2020 17:59:18 +0000 |
| Subject: | Doc #79352 [Opn]: ArrayObject::serialize and ArrayObject::unserialize are no longer overrideable | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-17381@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79352&edit=1
ID: 79352
Updated by: nikic@php.net
Reported by: tarasov dot igor at gmail dot com
Summary: ArrayObject::serialize and ArrayObject::unserialize
are no longer overrideable
Status: Open
Type: Documentation Problem
Package: SPL related
Operating System: Linux
PHP Version: 7.4.3
Block user comment: N
Private report: N
New Comment:
> So, then shouldn't be ArrayObject::serialize and ArrayObject::unserialize be declared
> final starting with PHP 7.4?
You may with to override both __serialize()/__unserialize() and Serializable if you care about
decoding payloads from previous versions, or being compatible with prior PHP versions. If you only
care about PHP 7.4, then overriding __unserialize() is sufficient, and you can ignore Serializable
completely.
> There is a mention in backward-incompatible changes
> https://www.php.net/manual/en/migration74.incompatible.php#migration74.incompatible.spl
> of ArrayObject, but it only states that ArrayObject and some other SPL classes "now support the
> __serialize() and __unserialize() mechanism in addition to the Serializable interface". This
> wording doesn't make it feel like there is something backward-incompatible.
Yes, this is indeed missing, thus the doc bug classification :)
Previous Comments:
------------------------------------------------------------------------
[2020-03-07 11:06:50] tarasov dot igor at gmail dot com
So, then shouldn't be ArrayObject::serialize and ArrayObject::unserialize be declared final
starting with PHP 7.4?
There is a mention in backward-incompatible changes https://www.php.net/manual/en/migration74.incompatible.php#migration74.incompatible.spl
of ArrayObject, but it only states that ArrayObject and some other SPL classes "now support the
__serialize() and __unserialize() mechanism in addition to the Serializable interface". This
wording doesn't make it feel like there is something backward-incompatible.
------------------------------------------------------------------------
[2020-03-07 09:48:12] nikic@php.net
You need to override __serialize() and __unserialize(). The Serializable implementation only exists
to decode payloads from old PHP versions, and will only be used in that case.
Reclassifying as a documentation problem.
------------------------------------------------------------------------
[2020-03-07 09:43:44] tarasov dot igor at gmail dot com
Description:
------------
If you inherit from ArrayObject and then try to override serialize and unserialize methods,
you'll notice that starting with PHP 7.4 these are not executed at all. There are no mentions
in the documentation describing this change. Sure, there are new __serialize and __unserialize, but
this backward-incompatible change and is not described anywhere.
If you create a custom class implementing Serializable interface, you can still override these
methods in child classes, no problem there.
Not sure if this change affects only ArrayObject, I suspect there might be other built-in classes
that were changed the same way.
Test script:
---------------
class Foo extends \ArrayObject {
public function serialize() {
return 'test';
}
}
echo serialize(new Foo);
Expected result:
----------------
C:3:"Foo":4:{test}
Actual result:
--------------
O:3:"Foo":3:{i:0;i:0;i:1;a:0:{}i:2;a:0:{}}
So, Foo::serialize is never executed. And it's not like the results are ignored, you can even
change it's body to die statement and it won't stop there.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79352&edit=1