Skip to content

Commit 30cc0c1

Browse files
committed
Merge branch 'PHP-8.1'
2 parents 76fcd70 + 49a3cc6 commit 30cc0c1

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

ext/date/php_date.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4522,6 +4522,20 @@ PHP_METHOD(DatePeriod, __construct)
45224522
}
45234523
dpobj->start_ce = date_ce_date;
45244524
} else {
4525+
/* Sanity checks */
4526+
if (start && Z_OBJCE_P(start) != date_ce_date && Z_OBJCE_P(start) != date_ce_immutable) {
4527+
zend_string *func = get_active_function_or_method_name();
4528+
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));
4529+
zend_string_release(func);
4530+
RETURN_THROWS();
4531+
}
4532+
if (end && Z_OBJCE_P(end) != date_ce_date && Z_OBJCE_P(end) != date_ce_immutable) {
4533+
zend_string *func = get_active_function_or_method_name();
4534+
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));
4535+
zend_string_release(func);
4536+
RETURN_THROWS();
4537+
}
4538+
45254539
/* init */
45264540
php_interval_obj *intobj = Z_PHPINTERVAL_P(interval);
45274541

ext/date/tests/bug80042.phpt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
Bug #80047: DatePeriod doesn't support custom DateTimeImmutable
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
class CustomDateTime extends DateTime {}
8+
class CustomDateTimeImmutable extends DateTimeImmutable {}
9+
10+
$dt = new DateTime('2022-06-24');
11+
$dti = new DateTimeImmutable('2022-06-24');
12+
$cdt = new CustomDateTime('2022-06-24');
13+
$cdti = new CustomDateTimeImmutable('2022-06-24');
14+
$i = new DateInterval('P1D');
15+
16+
$tests = [
17+
[ $dt, $i, $cdt ],
18+
[ $cdt, $i, $dt ],
19+
[ $cdt, $i, $cdt ],
20+
[ $dti, $i, $cdti ],
21+
[ $cdti, $i, $dti ],
22+
[ $cdti, $i, $cdti ],
23+
[ $cdt, $i, $cdti ],
24+
];
25+
26+
foreach ($tests as $test) {
27+
try {
28+
$dp = new DatePeriod(...$test);
29+
} catch ( Exception $e ) {
30+
echo $e->getMessage(), "\n";
31+
}
32+
}
33+
?>
34+
--EXPECT--
35+
DatePeriod::__construct(): Class of end date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided
36+
DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided
37+
DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided
38+
DatePeriod::__construct(): Class of end date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTimeImmutable provided
39+
DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTimeImmutable provided
40+
DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTimeImmutable provided
41+
DatePeriod::__construct(): Class of start date must be exactly DateTime or DateTimeImmutable, object of class CustomDateTime provided

0 commit comments

Comments
 (0)