This is an old revision of the document!
PHP RFC: Phasing out Serializable
- Date: 2020-12-07
- Author: Nikita Popov nikic@php.net
- Status: Under Discussion
- Target Version: PHP 8.1
- Implementation: https://github.com/php/php-src/pull/6494
Introduction
The new custom object serialization mechanism RFC introduced new __serialize()
and __unserialize()
magic methods in PHP 7.4, with the intent of replacing the broken Serializable
interface. This RFC finalizes that work by laying out a plan for the eventual removal of Serializable
.
Please see the referenced RFC for a detailed discussion of why the Serializable
interface is broken and needs to be replaced. Since PHP 7.4 a robust alternative mechanism exists, but some of the motivating issues will only be resolved once support for Serializable
is dropped entirely.
Proposal
A class is “only Serializable” if it is non-abstract, implements Serializable
, and does not implement __serialize()
and __unserialize()
. Then:
- In PHP 8.1, an “only Serializable” class will throw a deprecation warning.
- In PHP 9.0, an “only Serializable” class will generate a compile-time error. All other implementations of
Serializable
will result in a deprecation warning. Additionally, payloads using theC
serialization format will fail to unserialize. - In PHP 10.0 the
Serializable
interface will be removed.
The intent behind this deprecation timeline is to remove internal support for Serializable by PHP 9, but do this in a way that allows codebases to support multiple PHP versions easily, even across large ranges.
If a class implements both Serializable
and __serialize()
/__unserialize()
, the latter take precedence (on versions that support them), and the Serializable
interface is only used to decode existing serialization payload using the obsolete C
format. To migrate to the new mechanism, it's possible to either replace Serializable
entirely (if support for PHP 7.3 and below is not needed) or to implement both (if it is needed).
From an internal perspective, Serializable
support will be gone in PHP 9.0, and only a stub interface will be retained. PHP 10.0 removes that stub interface entirely. Only at that point will it no longer be trivial to support both PHP < 7.4 and >= 10.0, which should be a more than sufficient grace period.
Vote
Yes/No.