Bug #79041 [NEW]: PHP 7.4 changed foreach behavior for DateTime object
From: robert at korulczyk dot pl
Operating system:
PHP version: 7.4.1
Package: Date/time related
Bug Type: Bug
Bug description:PHP 7.4 changed foreach behavior for DateTime object
Description:
------------
In PHP 7.4 behavior of foreach (new DateTime() as $property => $value)
has changed. Prior to 7.4 foreach iterates through virtual properties of
DateTime object. Since 7.4 it is treated as empty array, which makes it
incosistent with json_encode() and (array) cast. Cast to array was
already fixed in https://bugs.php.net/bug.php?id=78383
https://3v4l.org/h99LLn
Context - foreach approach is used in Json helper in Yii 2:
https://github.com/yiisoft/yii2/issues/17760
https://github.com/yiisoft/yii2/blob/a636ff916ac3a06756f37c0dbfb55994394a6a55/framework/helpers/BaseJson.php#L160-L166
Test script:
---------------
$date = new DateTime('2019-12-24');
$properties = [];
foreach ($date as $k => $v) {
$properties[$k] = $v;
}
var_dump($date);
var_dump((array) $date);
var_dump($properties);
echo json_encode($date), "\n";
echo json_encode((array) $date), "\n";
echo json_encode($properties), "\n";
Expected result:
----------------
object(DateTime)#1 (3) {
["date"]=>
string(26) "2019-12-24 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
array(3) {
["date"]=>
string(26) "2019-12-24 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
array(3) {
["date"]=>
string(26) "2019-12-24 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
{"date":"2019-12-24
00:00:00.000000","timezone_type":3,"timezone":"Europe\/Amsterdam"}
{"date":"2019-12-24
00:00:00.000000","timezone_type":3,"timezone":"Europe\/Amsterdam"}
{"date":"2019-12-24
00:00:00.000000","timezone_type":3,"timezone":"Europe\/Amsterdam"}
Actual result:
--------------
object(DateTime)#1 (3) {
["date"]=>
string(26) "2019-12-24 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
array(3) {
["date"]=>
string(26) "2019-12-24 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
array(0) {
}
{"date":"2019-12-24
00:00:00.000000","timezone_type":3,"timezone":"Europe\/Amsterdam"}
{"date":"2019-12-24
00:00:00.000000","timezone_type":3,"timezone":"Europe\/Amsterdam"}
[]
--
Edit bug report at https://bugs.php.net/bug.php?id=79041&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79041&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79041&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79041&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79041&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79041&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79041&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79041&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79041&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79041&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79041&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79041&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79041&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79041&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79041&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79041&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79041&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79041&r=mysqlcfg
Thread (7 messages)
- robert at korulczyk dot pl