Bug #78383 [Nab]: Casting a \Datetime to array no longer returns it properties

From: Date: Wed, 07 Aug 2019 16:25:35 +0000
Subject: Bug #78383 [Nab]: Casting a \Datetime to array no longer returns it properties
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222124@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78383&edit=1 ID: 78383 Updated by: nikic@php.net Reported by: jeremy at derusse dot com Summary: Casting a \Datetime to array no longer returns it properties Status: Not a bug Type: Bug Package: Date/time related Operating System: Linux PHP Version: 7.4.0beta1 Assigned To: cmb Block user comment: N Private report: N New Comment: We could easily restore the previous behavior wrt array casts here -- we're already keeping compatibility with json_encode() for example. What's the use case? Previous Comments: ------------------------------------------------------------------------ [2019-08-07 16:21:11] cmb@php.net > Casting to an array is undefined, […] Not wrt. objects[1]: | An array converts to an object with properties named by keys and | corresponding values. Anyhow, this is a deliberate change[2]; DateTime objects are not supposed to have public properties. [1] <https://www.php.net/manual/en/language.types.object.php#language.types.object.casting> [2] <https://github.com/php/php-src/blob/be794894ef30764e54a8a52fa6c085f3c4a88b14/UPGRADING#L56-L57> ------------------------------------------------------------------------ [2019-08-07 16:02:26] duncan3dc@php.net Casting to an array is undefined, see here: https://www.php.net/manual/en/language.types.type-juggling.php ("The behaviour of an automatic conversion to array is currently undefined") ------------------------------------------------------------------------ [2019-08-07 13:03:07] jeremy at derusse dot com Description: ------------ Casting an instance of \Datetime to an array should return date's properties. Since PHP 7.4, the array is empty. see https://3v4l.org/epBRn Test script: --------------- var_dump((array) new \Datetime('2000-01-01')); Expected result: ---------------- array(3) { ["date"]=> string(26) "2000-01-01 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } Actual result: -------------- array(0) { } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78383&edit=1

« previous php.bugs (#222124) next »