xref: /PHP-8.3/ext/date/tests/gh10747-error.phpt (revision f575027b)
1--TEST--
2Bug GH-10747 (Private fields in serialized DateTimeImmutable objects throw)
3--FILE--
4<?php
5class I extends DateTimeImmutable
6{
7	private   int $var1 = 1;
8	private       $var2 = 0;
9	protected int $var3 = 3;
10	protected     $var4;
11
12	function __construct()
13	{
14		parent::__construct();
15		$this->var2 = 2;
16		$this->var4 = 4;
17	}
18}
19
20$s = 'O:1:"I":7:{s:4:"date";s:26:"2023-03-09 17:03:11.123456";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";s:7:"!I!var1";i:1;s:7:"!I!var2";i:2;s:7:"!*!var3";i:3;s:7:"!*!var4";i:4;}';
21$u = unserialize(str_replace('!', chr(0), $s));
22var_dump($s, $u);
23
24// this one has the class names for the private properties changed to something non-existing
25$s = 'O:1:"I":7:{s:4:"date";s:26:"2023-03-09 17:03:11.123456";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";s:7:"!J!var1";i:1;s:7:"!K!var2";i:2;s:7:"!*!var3";i:3;s:7:"!*!var4";i:4;}';
26$u = unserialize(str_replace('!', chr(0), $s));
27var_dump($s, $u);
28
29?>
30--EXPECTF--
31string(179) "O:1:"I":7:{s:4:"date";s:26:"2023-03-09 17:03:11.123456";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";s:7:"!I!var1";i:1;s:7:"!I!var2";i:2;s:7:"!*!var3";i:3;s:7:"!*!var4";i:4;}"
32object(I)#1 (7) {
33  ["var1":"I":private]=>
34  int(1)
35  ["var2":"I":private]=>
36  int(2)
37  ["var3":protected]=>
38  int(3)
39  ["var4":protected]=>
40  int(4)
41  ["date"]=>
42  string(26) "2023-03-09 17:03:11.123456"
43  ["timezone_type"]=>
44  int(3)
45  ["timezone"]=>
46  string(3) "UTC"
47}
48string(179) "O:1:"I":7:{s:4:"date";s:26:"2023-03-09 17:03:11.123456";s:13:"timezone_type";i:3;s:8:"timezone";s:3:"UTC";s:7:"!J!var1";i:1;s:7:"!K!var2";i:2;s:7:"!*!var3";i:3;s:7:"!*!var4";i:4;}"
49object(I)#2 (7) {
50  ["var1":"I":private]=>
51  int(1)
52  ["var2":"I":private]=>
53  int(0)
54  ["var3":protected]=>
55  int(3)
56  ["var4":protected]=>
57  int(4)
58  ["date"]=>
59  string(26) "2023-03-09 17:03:11.123456"
60  ["timezone_type"]=>
61  int(3)
62  ["timezone"]=>
63  string(3) "UTC"
64}
65