Skip to content

Commit 3e6b447

Browse files
committed
Partially deprecate Serializable
If Serializable is implemented, require that __serialize() and __unserialize() are implemented as well, else issue a deprecation warning. Also deprecate use of PDO::FETCH_SERIALIZE. RFC: https://wiki.php.net/rfc/phase_out_serializable Closes GH-6494.
1 parent 5295e36 commit 3e6b447

32 files changed

+136
-28
lines changed

UPGRADING

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ PHP 8.1 UPGRADE NOTES
254254
4. Deprecated Functionality
255255
========================================
256256

257+
- Core:
258+
. Implementing the Serializable interface without also implementing
259+
__serialize() and __unserialize() has been deprecated. You should either
260+
implement the new methods (if you only support PHP 7.4 and higher) or
261+
implement both (if you support older PHP versions as well).
262+
RFC: https://wiki.php.net/rfc/phase_out_serializable
263+
257264
- MySQLi:
258265
. The mysqli_driver::$driver_version property has been deprecated. The driver
259266
version is meaningless as it hasn't been updated in more than a decade. Use
@@ -263,6 +270,10 @@ PHP 8.1 UPGRADE NOTES
263270
mysqli_get_client_info() without any arguments to obtain the client
264271
library version information.
265272

273+
- PDO:
274+
. The PDO::FETCH_SERIALIZE mode has been deprecated.
275+
RFC: https://wiki.php.net/rfc/phase_out_serializable
276+
266277
========================================
267278
5. Changed Functions
268279
========================================

Zend/tests/bug64354.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ try {
2020
var_dump($e->getMessage());
2121
}
2222
?>
23-
--EXPECT--
23+
--EXPECTF--
24+
Deprecated: The Serializable interface is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
2425
string(9) "serialize"

Zend/tests/enum/no-implement-serializable-indirect.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ var_dump(unserialize(serialize(Foo::Bar)));
2121

2222
?>
2323
--EXPECTF--
24+
Deprecated: The Serializable interface is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
25+
2426
Fatal error: Enums may not implement the Serializable interface in %s on line %d

Zend/tests/enum/no-implement-serializable.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ var_dump(unserialize(serialize(Foo::Bar)));
1919

2020
?>
2121
--EXPECTF--
22+
Deprecated: The Serializable interface is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
23+
2224
Fatal error: Enums may not implement the Serializable interface in %s on line %d
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Serializable deprecation
3+
--FILE--
4+
<?php
5+
6+
interface I extends Serializable {}
7+
abstract class A implements Serializable {}
8+
9+
class C extends A implements I {
10+
public function serialize(): string {}
11+
public function unserialize(string $data) {}
12+
}
13+
14+
class D extends A implements I {
15+
public function serialize(): string {}
16+
public function unserialize(string $data) {}
17+
public function __serialize(): array {}
18+
public function __unserialize(array $data) {}
19+
}
20+
21+
?>
22+
--EXPECTF--
23+
Deprecated: The Serializable interface is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d

Zend/tests/traits/interface_003.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var_dump(unserialize($o));
2121

2222
?>
2323
--EXPECTF--
24+
Deprecated: The Serializable interface is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
2425
string(20) "C:3:"bar":6:{foobar}"
2526
string(6) "foobar"
2627
object(bar)#%d (0) {

Zend/zend_interfaces.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ static int zend_implement_serializable(zend_class_entry *interface, zend_class_e
432432
if (!class_type->unserialize) {
433433
class_type->unserialize = zend_user_unserialize;
434434
}
435+
if (!(class_type->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)
436+
&& (!class_type->__serialize || !class_type->__unserialize)) {
437+
zend_error(E_DEPRECATED, "The Serializable interface is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary)");
438+
}
435439
return SUCCESS;
436440
}
437441
/* }}}*/

ext/pdo/pdo_stmt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,9 @@ static bool pdo_stmt_verify_mode(pdo_stmt_t *stmt, zend_long mode, uint32_t mode
11411141
ZEND_FALLTHROUGH;
11421142

11431143
case PDO_FETCH_CLASS:
1144+
if (flags & PDO_FETCH_SERIALIZE) {
1145+
php_error_docref(NULL, E_DEPRECATED, "The PDO::FETCH_SERIALIZE mode is deprecated");
1146+
}
11441147
return 1;
11451148
}
11461149
}

ext/pdo/tests/bug_44409.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ $stmt = $db->query("SELECT * FROM test");
4040
print_r($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_SERIALIZE, "bug44409"));
4141

4242
?>
43-
--EXPECT--
43+
--EXPECTF--
44+
Deprecated: The Serializable interface is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
45+
46+
Deprecated: PDOStatement::fetchAll(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d
4447
Method called: bug44409::unserialize('Data from DB')
4548
Array
4649
(

ext/pdo/tests/pdo_018.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,11 @@ var_dump($stmt->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_CLASSTYPE|PDO::FETCH_SERIAL
183183

184184
?>
185185
--EXPECTF--
186+
Deprecated: The Serializable interface is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
187+
188+
Deprecated: The Serializable interface is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
189+
190+
Deprecated: The Serializable interface is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in %s on line %d
186191
string(1) "3"
187192
array(3) {
188193
[0]=>
@@ -221,6 +226,8 @@ array(4) {
221226
string(172) "a:5:{s:7:"BasePub";s:13:"DerivedPublic";s:7:"BasePro";s:16:"DerivdeProtected";s:7:"BasePri";s:7:"Private";s:10:"DerivedPub";s:6:"Public";s:10:"DerivedPro";s:9:"Protected";}"
222227
}
223228
===FAILURE===
229+
230+
Deprecated: PDOStatement::fetchAll(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d
224231
Exception:SQLSTATE[HY000]: General error: cannot unserialize class
225232
===COUNT===
226233
string(1) "3"
@@ -249,6 +256,8 @@ array(3) {
249256
}
250257
}
251258
===FETCHCLASS===
259+
260+
Deprecated: PDOStatement::fetchAll(): The PDO::FETCH_SERIALIZE mode is deprecated in %s on line %d
252261
TestBase::unserialize(a:3:{s:7:"BasePub";s:6:"Public";s:7:"BasePro";s:9:"Protected";s:7:"BasePri";s:7:"Private";})
253262
TestDerived::unserialize()
254263
TestBase::unserialize(a:5:{s:7:"BasePub";s:13:"DerivedPublic";s:7:"BasePro";s:16:"DerivdeProtected";s:7:"BasePri";s:7:"Private";s:10:"DerivedPub";s:6:"Public";s:10:"DerivedPro";s:9:"Protected";})

0 commit comments

Comments
 (0)