Bug #78195 [Opn]: Datetime object coverts to empty arrays

From: Date: Fri, 21 Jun 2019 21:20:00 +0000
Subject: Bug #78195 [Opn]: Datetime object coverts to empty arrays
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-221446@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78195&edit=1 ID: 78195 User updated by: v-yitam at microsoft dot com Reported by: v-yitam at microsoft dot com Summary: Datetime object coverts to empty arrays Status: Open Type: Bug Package: Class/Object related Operating System: Irrelevant PHP Version: 7.4.0alpha1 Block user comment: N Private report: N New Comment: I see. Yes I did check the upgrade notes etc. but didn't find anything usefully related. Thanks for your prompt response! Previous Comments: ------------------------------------------------------------------------ [2019-06-21 21:11:13] nikic@php.net This is an intentional change. These are not real properties, but debug output. You'll note that writing something like var_dump($date->timezone_type) will give you an undefined property error. The userland analogue to this is an object with a __debugInfo() method -- this will show up in var_dump() output, but will not affect an (array) cast. The DateTime object is now consistent with userland behavior. The UPGRADING entry https://github.com/php/php-src/blob/d3112adf7f22a6eede0c90218ffd1badcaaad1a0/UPGRADING#L48-L49 should be expanded to mention this -- only part of the change is described right now. ------------------------------------------------------------------------ [2019-06-21 20:59:13] v-yitam at microsoft dot com Description: ------------ This seemingly happens with datetime objects only, not with simple user defined objects. Reproducible with PHP 7.4 Alpha 1 but not with PHP 7.3.6 Test script: --------------- <?php class foo { public $a; public $b; } $bar = new foo; $bar->a = 'A'; $bar->b = 'B'; var_dump($bar); $arr = (array) $bar; var_dump($arr); $date = date_create('2018-12-31 23:59:59.997'); date_default_timezone_set("America/Los_Angeles"); var_dump($date); $darr = (array) $date; var_dump($darr); ?> Expected result: ---------------- object(foo)#1 (2) { ["a"]=> string(1) "A" ["b"]=> string(1) "B" } array(2) { ["a"]=> string(1) "A" ["b"]=> string(1) "B" } object(DateTime)#2 (3) { ["date"]=> string(26) "2018-12-31 23:59:59.997000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } array(3) { ["date"]=> string(26) "2018-12-31 23:59:59.997000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } Actual result: -------------- object(foo)#1 (2) { ["a"]=> string(1) "A" ["b"]=> string(1) "B" } array(2) { ["a"]=> string(1) "A" ["b"]=> string(1) "B" } object(DateTime)#2 (3) { ["date"]=> string(26) "2018-12-31 23:59:59.997000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } array(0) { } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78195&edit=1

« previous php.bugs (#221446) next »