DateTimeZone object is unconstructed¶
Description¶
IntlTimeZone::fromDateTimeZone() converts a DateTimeZone object into an IntlTimeZone one. To do so, it reads the internal timezone data that DateTimeZone::__construct() normally sets up.
If it is given a DateTimeZone instance that was created without going through its constructor – for example via ReflectionClass::newInstanceWithoutConstructor(), or an object left in a broken state after a failed unserialize() – that internal data is missing, and the conversion cannot proceed.
Example¶
<?php
$tz = (new ReflectionClass(DateTimeZone::class))->newInstanceWithoutConstructor();
// The object exists, but __construct() was never called
IntlTimeZone::fromDateTimeZone($tz);
?>
Solutions¶
Always create the
DateTimeZonewithnew DateTimeZone($identifier)rather than bypassing its constructor.If the object comes from unserialization, verify it succeeded and produced a valid object before using it.
Wrap the call in a
try/catch(Error) if theDateTimeZoneinstance’s origin cannot be controlled.
See Also¶
Changed Behavior¶
This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.