Bug #76057 [Com]: For DateTime the properties 'timezone' and 'timezone_type' are not set on init

From: Date: Thu, 08 Mar 2018 18:34:56 +0000
Subject: Bug #76057 [Com]: For DateTime the properties 'timezone' and 'timezone_type' are not set on init
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-214239@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76057&edit=1

 ID:                 76057
 Comment by:         projectcleverweb at gmail dot com
 Reported by:        projectcleverweb at gmail dot com
 Summary:            For DateTime the properties 'timezone' and
                     'timezone_type' are not set on init
 Status:             Open
 Type:               Bug
 Package:            Date/time related
 Operating System:   Linux
 PHP Version:        7.2.3
 Block user comment: N
 Private report:     N

 New Comment:

Ok then, if they are not intended to be used they both should simply be protected or private
properties; however, in this case how are you supposed to access $timezone_type?

If you are writing your own serialization method (or in my case an extension of DateTime) you would
have to make an exception for DateTime because even though it has a __set_state() method, that
method requires the value of $timezone_type.

See: https://3v4l.org/Klvo3

Ideally DateTimeZone (and possibly DateTime) would have a getTimezoneType() method.


Previous Comments:
------------------------------------------------------------------------
[2018-03-08 14:49:17] jhdxr@php.net

This behavior (get timezone via property) is not expected, you should use getTimezone()
to retrieve timezone.

------------------------------------------------------------------------
[2018-03-05 21:04:46] projectcleverweb at gmail dot com

Description:
------------
In short, these to properties are not set when the class is instantiated, but do become set when the
class is serialized OR when the property list is requested (such as via get_object_vars()).

You can see the conditions when timezone_type will be set on this page by just searching in the
browser for "timezone_type"

https://github.com/php/php-src/blob/ef255c9f0f2c2ad1ea224ed215edb00f5bc205bd/ext/date/php_date.c#L2269

This issue is occurring on PHP 5.6 and later (shown in the test script URL)

Test script:
---------------
https://3v4l.org/ZhQX9

--- OR ---

$test = new DateTime('2018-03-05 00:00:00', new DateTimeZone('UTC'));
var_dump($test->timezone); // Notice: Undefined property: DateTime::$timezone
get_object_vars($test);
var_dump($test->timezone); // string(3) "UTC"

Expected result:
----------------
string(3) "UTC"
string(3) "UTC"

Actual result:
--------------
Notice: Undefined property: DateTime::$timezone in /in/BQD1t on line 5
NULL
string(3) "UTC"


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=76057&edit=1


Thread (6 messages)

« previous php.bugs (#214239) next »