php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78195 Datetime object coverts to empty arrays
Submitted: 2019-06-21 20:59 UTC Modified: 2019-06-21 23:05 UTC
From: v-yitam at microsoft dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 7.4.0alpha1 OS: Irrelevant
Private report: No CVE-ID: None
 [2019-06-21 20:59 UTC] 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) {
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-06-21 21:11 UTC] 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 21:20 UTC] v-yitam at microsoft dot com
I see. Yes I did check the upgrade notes etc. but didn't find anything usefully related.
Thanks for your prompt response!
 [2019-06-21 23:05 UTC] daverandom@php.net
-Status: Open +Status: Not a bug
 [2019-06-21 23:05 UTC] daverandom@php.net
Expected behaviour, see comments
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 06:01:30 2024 UTC