Bug #78383 [Fbk->Asn]: Casting a \Datetime to array no longer returns it properties

From: Date: Wed, 07 Aug 2019 20:41:48 +0000
Subject: Bug #78383 [Fbk->Asn]: Casting a \Datetime to array no longer returns it properties
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222128@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 User updated by: jeremy at derusse dot com Reported by: jeremy at derusse dot com Summary: Casting a \Datetime to array no longer returns it properties -Status: Feedback +Status: Assigned Type: Bug Package: Date/time related Operating System: Linux PHP Version: 7.4.0beta1 Assigned To: nikic Block user comment: N Private report: N New Comment: We (symfony) implemented a generic serialization mechanism that uses array casts to extract state from objects. DateTime was not needing any special care, but now it would because the behavior changed between php 7.3 and 7.4, IMHO, it's a BC break. Not https://3v4l.org/rdhKg: another change in the bevahior of \Datetime casting. Previous Comments: ------------------------------------------------------------------------ [2019-08-07 16:30:17] cmb@php.net Well, then ... :) ------------------------------------------------------------------------ [2019-08-07 16:25:35] nikic@php.net 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? ------------------------------------------------------------------------ [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 (#222128) next »