.. _datetimezone-object-is-unconstructed:
DateTimeZone object is unconstructed
------------------------------------
.. meta::
:description:
DateTimeZone object is unconstructed: ``IntlTimeZone::fromDateTimeZone()`` converts a ``DateTimeZone`` object into an ``IntlTimeZone`` one.
:og:image: https://php-errors.readthedocs.io/en/latest/_static/logo.png
:og:type: article
:og:title: DateTimeZone object is unconstructed
:og:description: ``IntlTimeZone::fromDateTimeZone()`` converts a ``DateTimeZone`` object into an ``IntlTimeZone`` one
:og:url: https://php-errors.readthedocs.io/en/latest/messages/datetimezone-object-is-unconstructed.html
:og:locale: en
:twitter:card: summary_large_image
:twitter:site: @exakat
:twitter:title: DateTimeZone object is unconstructed
:twitter:description: DateTimeZone object is unconstructed: ``IntlTimeZone::fromDateTimeZone()`` converts a ``DateTimeZone`` object into an ``IntlTimeZone`` one
:twitter:creator: @exakat
:twitter:image:src: https://php-errors.readthedocs.io/en/latest/_static/logo.png
.. raw:: html
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
_______
.. code-block:: php
newInstanceWithoutConstructor();
// The object exists, but __construct() was never called
IntlTimeZone::fromDateTimeZone($tz);
?>
Solutions
_________
+ Always create the ``DateTimeZone`` with ``new 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 the ``DateTimeZone`` instance's origin cannot be controlled.
Related Error Messages
______________________
+ :ref:`the-sqlite3-object-has-not-been-correctly-initialised-or-is-already-closed`
+ :ref:`intllistformatter-object-is-already-constructed`
See Also
________
+ `IntlTimeZone::fromDateTimeZone `_
+ `DateTimeZone `_
Changed Behavior
________________
This error may appear following an evolution in behavior, in previous versions. See ` `_.