php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78383 Casting a DateTime to array no longer returns its properties
Submitted: 2019-08-07 13:03 UTC Modified: 2019-08-08 08:23 UTC
From: jeremy at derusse dot com Assigned: nikic (profile)
Status: Closed Package: Date/time related
PHP Version: 7.4.0beta1 OS: Linux
Private report: No CVE-ID: None
 [2019-08-07 13:03 UTC] 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) {
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-08-07 16:02 UTC] 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 16:21 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-08-07 16:21 UTC] 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:25 UTC] 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:30 UTC] cmb@php.net
-Status: Not a bug +Status: Feedback -Assigned To: cmb +Assigned To: nikic
 [2019-08-07 16:30 UTC] cmb@php.net
Well, then ... :)
 [2019-08-07 20:41 UTC] jeremy at derusse dot com
-Status: Feedback +Status: Assigned
 [2019-08-07 20:41 UTC] jeremy at derusse dot com
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.
 [2019-08-08 07:53 UTC] nikic@php.net
-Summary: Casting a \Datetime to array no longer returns it properties +Summary: Casting a DateTime to array no longer returns its properties
 [2019-08-08 07:56 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=fc17bfaacfd48b3952652792bf684ff223499678
Log: Fixed bug #78383
 [2019-08-08 07:56 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2019-08-08 08:23 UTC] jeremy at derusse dot com
Thank you @nikic
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC