Skip to content

Commit 89c327f

Browse files
committed
Fix #78751: Serialising DatePeriod converts DateTimeImmutable
When getting the properties of a DatePeriod instance we have to retain the proper classes, and when restoring a DatePeriod instance we have to cater to DateTimeImmutable instances as well.
1 parent 16c4910 commit 89c327f

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ PHP NEWS
1313

1414
- Date:
1515
. Fixed bug #70153 (\DateInterval incorrectly unserialized). (Maksim Iakunin)
16+
. Fixed bug #78751 (Serialising DatePeriod converts DateTimeImmutable). (cmb)
1617

1718
- Iconv:
1819
. Fixed bug #78642 (Wrong libiconv version displayed). (gedas at martynas,

ext/date/php_date.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5171,7 +5171,7 @@ static HashTable *date_object_get_properties_period(zval *object) /* {{{ */
51715171

51725172
if (period_obj->start) {
51735173
php_date_obj *date_obj;
5174-
object_init_ex(&zv, date_ce_date);
5174+
object_init_ex(&zv, period_obj->start_ce);
51755175
date_obj = Z_PHPDATE_P(&zv);
51765176
date_obj->time = timelib_time_clone(period_obj->start);
51775177
} else {
@@ -5181,7 +5181,7 @@ static HashTable *date_object_get_properties_period(zval *object) /* {{{ */
51815181

51825182
if (period_obj->current) {
51835183
php_date_obj *date_obj;
5184-
object_init_ex(&zv, date_ce_date);
5184+
object_init_ex(&zv, period_obj->start_ce);
51855185
date_obj = Z_PHPDATE_P(&zv);
51865186
date_obj->time = timelib_time_clone(period_obj->current);
51875187
} else {
@@ -5191,7 +5191,7 @@ static HashTable *date_object_get_properties_period(zval *object) /* {{{ */
51915191

51925192
if (period_obj->end) {
51935193
php_date_obj *date_obj;
5194-
object_init_ex(&zv, date_ce_date);
5194+
object_init_ex(&zv, period_obj->start_ce);
51955195
date_obj = Z_PHPDATE_P(&zv);
51965196
date_obj->time = timelib_time_clone(period_obj->end);
51975197
} else {
@@ -5228,7 +5228,7 @@ static int php_date_period_initialize_from_hash(php_period_obj *period_obj, Hash
52285228

52295229
ht_entry = zend_hash_str_find(myht, "start", sizeof("start")-1);
52305230
if (ht_entry) {
5231-
if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_date) {
5231+
if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
52325232
php_date_obj *date_obj;
52335233
date_obj = Z_PHPDATE_P(ht_entry);
52345234
period_obj->start = timelib_time_clone(date_obj->time);
@@ -5242,7 +5242,7 @@ static int php_date_period_initialize_from_hash(php_period_obj *period_obj, Hash
52425242

52435243
ht_entry = zend_hash_str_find(myht, "end", sizeof("end")-1);
52445244
if (ht_entry) {
5245-
if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_date) {
5245+
if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
52465246
php_date_obj *date_obj;
52475247
date_obj = Z_PHPDATE_P(ht_entry);
52485248
period_obj->end = timelib_time_clone(date_obj->time);
@@ -5255,7 +5255,7 @@ static int php_date_period_initialize_from_hash(php_period_obj *period_obj, Hash
52555255

52565256
ht_entry = zend_hash_str_find(myht, "current", sizeof("current")-1);
52575257
if (ht_entry) {
5258-
if (Z_TYPE_P(ht_entry) == IS_OBJECT && Z_OBJCE_P(ht_entry) == date_ce_date) {
5258+
if (Z_TYPE_P(ht_entry) == IS_OBJECT && instanceof_function(Z_OBJCE_P(ht_entry), date_ce_interface)) {
52595259
php_date_obj *date_obj;
52605260
date_obj = Z_PHPDATE_P(ht_entry);
52615261
period_obj->current = timelib_time_clone(date_obj->time);

ext/date/tests/bug78751.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Bug #78751 (Serialising DatePeriod converts DateTimeImmutable)
3+
--FILE--
4+
<?php
5+
$oDay = new DateTimeImmutable('2019-10-25');
6+
$oDateInterval = DateInterval::createFromDateString('1 day');
7+
$oDays = new DatePeriod($oDay, $oDateInterval, $oDay->modify('+1 day'));
8+
$oDays = unserialize(serialize($oDays));
9+
var_dump(
10+
$oDays->start instanceof DateTimeImmutable,
11+
$oDays->end instanceof DateTimeImmutable
12+
);
13+
?>
14+
--EXPECT--
15+
bool(true)
16+
bool(true)

0 commit comments

Comments
 (0)