Skip to content

Partially deprecate Serializable #6494

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 3 commits into from

Conversation

nikic
Copy link
Member

@nikic nikic commented Dec 7, 2020

RFC: https://wiki.php.net/rfc/phase_out_serializable

If Serializable is implemented, require that __serialize() and
__unserialize() are implemented as well, else issue a deprecation
warning.

@nikic nikic added the RFC label Dec 7, 2020
@nikic nikic added this to the PHP 8.1 milestone Dec 7, 2020
@nikic
Copy link
Member Author

nikic commented Dec 7, 2020

Hm, looks like PDO::FETCH_SERIALIZE is based on Serializable :/

@nikic nikic force-pushed the deprecate-serializable branch from 31ccf91 to 807e5c2 Compare December 7, 2020 15:27

?>
--EXPECTF--
Deprecated: The Serializable interface is deprecated. If you need to retain the Serializable interface for cross-version compatibility, you can suppress this warning by implementing __serialize() and __unserialize() in addition, which will take precedence over Serializable in PHP versions that support them in %s on line 6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Deprecated: The Serializable interface is deprecated. If you need to retain the Serializable interface for cross-version compatibility, you can suppress this warning by implementing __serialize() and __unserialize() in addition, which will take precedence over Serializable in PHP versions that support them in %s on line 6
Deprecated: The Serializable interface is deprecated. For cross-version compatibility, also implement __serialize() and __unserialize() which take precedente over Serializable in %s on line 6

Copy link
Member Author

@nikic nikic Dec 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Deprecated: The Serializable interface is deprecated. If you need to retain the Serializable interface for cross-version compatibility, you can suppress this warning by implementing __serialize() and __unserialize() in addition, which will take precedence over Serializable in PHP versions that support them in %s on line 6
Deprecated: The Serializable interface is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions or serialization payloads needs to be retained) in %s on line 6

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the main thing I want to convey here is that the alternative is __serialize/__unserialize, and you only need both for supporting multiple versions, otherwise you don't.

@nikic nikic force-pushed the deprecate-serializable branch from 807e5c2 to 2d81670 Compare April 28, 2021 12:27
nikic added 2 commits April 28, 2021 14:59
If Serializable is implemented, require that __serialize() and
__unserialize() are implemented as well, else issue a deprecation
warning.
@nikic nikic force-pushed the deprecate-serializable branch from 2d81670 to f811e57 Compare April 28, 2021 13:02
@nikic nikic force-pushed the deprecate-serializable branch from f811e57 to 9154c53 Compare April 28, 2021 13:40
@nikic nikic closed this in 3e6b447 Apr 28, 2021
wouterj added a commit to wouterj/php-src that referenced this pull request Aug 5, 2021
nikic pushed a commit that referenced this pull request Aug 11, 2021
The deprecation message was originally introduced in 3e6b447 (#6494).

I first encountered this notice when testing the MongoDB extension
with PHP 8.1, which produced many duplicate messages that provided
no detail about the particular class that needed to be fixed.

Closes GH-7346.
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Mar 9, 2022
New sniff which addresses the following deprecation in PHP 8.1:

> **Implementing Serializable without `__serialize()` and `__unserialize()`**
>
> Either only the new methods should be implemented, if no support for PHP prior to version 7.4 is provided, or both should be implemented.

This sniff will throw a warning when a class implements the `Serializable` interface, but does not implement the magic `__serialize()` and `__unserialize()` methods.

The sniff contains a `public` `$serializableInterfaces` property for users to provide their own list of additional interfaces (which extend the `Serializable` interface) to scan for.

If the sniff comes across an interface declaration extending the `Serializable` interface and the interface is not in the user provided `$serializableInterfaces` list, a warning will be thrown for the user to add the interface to the property via a custom ruleset.

Additionally, when the `testVersion` has a minimum PHP version of `7.4` or higher, an additional check is executed:
* For interfaces directly extending `Serializable` AND containing a declaration of both the `__serialize()` as well as the `__unserialize()` magic method, thus enforcing the implementation of these methods in classes implementing the interface, a warning will be thrown that the extension of the `Serializable` interface can be removed.
* For classes which implement the `Serializable` interface AND contain a declaration of both the `__serialize()` as well as the `__unserialize()` magic method, a warning will be thrown that the implementation of the `Serializable` interface can be removed.

Includes unit tests.
Includes updating the section about available custom properties in the README.

Refs:
 * https://www.php.net/manual/en/migration81.deprecated.php#migration81.deprecated.core.serialize-interface
 * https://wiki.php.net/rfc/phase_out_serializable
 * https://www.php.net/manual/en/class.serializable.php
 * https://www.php.net/manual/en/language.oop5.magic.php#object.serialize
 * php/php-src#6494
 * php/php-src@3e6b447
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Mar 9, 2022
New sniff which addresses the following deprecation in PHP 8.1:

> **Implementing Serializable without `__serialize()` and `__unserialize()`**
>
> Either only the new methods should be implemented, if no support for PHP prior to version 7.4 is provided, or both should be implemented.

This sniff will throw a warning when a class implements the `Serializable` interface, but does not implement the magic `__serialize()` and `__unserialize()` methods.

The sniff contains a `public` `$serializableInterfaces` property for users to provide their own list of additional interfaces (which extend the `Serializable` interface) to scan for.

If the sniff comes across an interface declaration extending the `Serializable` interface and the interface is not in the user provided `$serializableInterfaces` list, a warning will be thrown for the user to add the interface to the property via a custom ruleset.

Additionally, when the `testVersion` has a minimum PHP version of `7.4` or higher, an additional check is executed:
* For interfaces directly extending `Serializable` AND containing a declaration of both the `__serialize()` as well as the `__unserialize()` magic method, thus enforcing the implementation of these methods in classes implementing the interface, a warning will be thrown that the extension of the `Serializable` interface can be removed.
* For classes which implement the `Serializable` interface AND contain a declaration of both the `__serialize()` as well as the `__unserialize()` magic method, a warning will be thrown that the implementation of the `Serializable` interface can be removed.

Includes unit tests.
Includes updating the section about available custom properties in the README.

Refs:
 * https://www.php.net/manual/en/migration81.deprecated.php#migration81.deprecated.core.serialize-interface
 * https://wiki.php.net/rfc/phase_out_serializable
 * https://www.php.net/manual/en/class.serializable.php
 * https://www.php.net/manual/en/language.oop5.magic.php#object.serialize
 * php/php-src#6494
 * php/php-src@3e6b447
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.

2 participants