diff --git a/NEWS b/NEWS index e63742c441237..8cbac266d73c4 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,10 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? 2022, PHP 8.0.23 +- Date: + . Fixed bug #80047 (DatePeriod doesn't warn with custom DateTimeImmutable). + (Derick) + - DBA: . Fixed LMDB driver memory leak on DB creation failure (Girgias) . Fixed bug GH-9155 (dba_open("non-existing", "c-", "flatfile") segfaults). diff --git a/ext/date/php_date.c b/ext/date/php_date.c index 3c26c11e1035d..7fb538e380ccd 100644 --- a/ext/date/php_date.c +++ b/ext/date/php_date.c @@ -1475,6 +1475,17 @@ static int date_period_it_has_more(zend_object_iterator *iter) } /* }}} */ +static zend_class_entry *get_base_date_class(zend_class_entry *start_ce) +{ + zend_class_entry *tmp = start_ce; + + while (tmp != date_ce_date && tmp != date_ce_immutable && tmp->parent) { + tmp = tmp->parent; + } + + return tmp; +} + /* {{{ date_period_it_current_data */ static zval *date_period_it_current_data(zend_object_iterator *iter) { @@ -1484,7 +1495,7 @@ static zval *date_period_it_current_data(zend_object_iterator *iter) php_date_obj *newdateobj; /* Create new object */ - php_date_instantiate(object->start_ce, &iterator->current); + php_date_instantiate(get_base_date_class(object->start_ce), &iterator->current); newdateobj = Z_PHPDATE_P(&iterator->current); newdateobj->time = timelib_time_ctor(); *newdateobj->time = *it_time; @@ -4227,20 +4238,6 @@ PHP_METHOD(DatePeriod, __construct) } dpobj->start_ce = date_ce_date; } else { - /* Sanity checks */ - if (start && Z_OBJCE_P(start) != date_ce_date && Z_OBJCE_P(start) != date_ce_immutable) { - zend_string *func = get_active_function_or_method_name(); - zend_throw_error(zend_ce_exception, "%s(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class %s provided", ZSTR_VAL(func), ZSTR_VAL(Z_OBJCE_P(start)->name)); - zend_string_release(func); - RETURN_THROWS(); - } - if (end && Z_OBJCE_P(end) != date_ce_date && Z_OBJCE_P(end) != date_ce_immutable) { - zend_string *func = get_active_function_or_method_name(); - zend_throw_error(zend_ce_exception, "%s(): Class of end date must be exactly DateTime or DateTimeImmutable, object of class %s provided", ZSTR_VAL(func), ZSTR_VAL(Z_OBJCE_P(end)->name)); - zend_string_release(func); - RETURN_THROWS(); - } - /* init */ php_interval_obj *intobj = Z_PHPINTERVAL_P(interval); diff --git a/ext/date/tests/bug80042.phpt b/ext/date/tests/bug80042.phpt deleted file mode 100644 index 8356d8693bafd..0000000000000 --- a/ext/date/tests/bug80042.phpt +++ /dev/null @@ -1,41 +0,0 @@ ---TEST-- -Bug #80047: DatePeriod doesn't support custom DateTimeImmutable ---INI-- -date.timezone=UTC ---FILE-- -getMessage(), "\n"; - } -} -?> ---EXPECT-- -DatePeriod::__construct(): Class of end date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided -DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided -DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided -DatePeriod::__construct(): Class of end date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTimeImmutable provided -DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTimeImmutable provided -DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTimeImmutable provided -DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided diff --git a/ext/date/tests/bug80047.phpt b/ext/date/tests/bug80047.phpt new file mode 100644 index 0000000000000..30f66375da884 --- /dev/null +++ b/ext/date/tests/bug80047.phpt @@ -0,0 +1,39 @@ +--TEST-- +Bug #80047: DatePeriod doesn't support custom DateTimeImmutable +--INI-- +date.timezone=UTC +--FILE-- + +--EXPECT-- +DateTime +DateTime +DateTime +DateTimeImmutable +DateTimeImmutable +DateTimeImmutable +DateTimeImmutable