.. _cannot-directly-construct-directory,-use-dir()-instead:
Cannot directly construct Directory, use dir() instead
------------------------------------------------------
.. meta::
:description:
Cannot directly construct Directory, use dir() instead: ``Directory`` is the object returned by the ``dir()`` function, giving access to ``path``, ``handle``, ``read()``, ``rewind()`` and ``close()``.
:og:image: https://php-errors.readthedocs.io/en/latest/_static/logo.png
:og:type: article
:og:title: Cannot directly construct Directory, use dir() instead
:og:description: ``Directory`` is the object returned by the ``dir()`` function, giving access to ``path``, ``handle``, ``read()``, ``rewind()`` and ``close()``
:og:url: https://php-errors.readthedocs.io/en/latest/messages/cannot-directly-construct-directory%2C-use-dir%28%29-instead.html
:og:locale: en
:twitter:card: summary_large_image
:twitter:site: @exakat
:twitter:title: Cannot directly construct Directory, use dir() instead
:twitter:description: Cannot directly construct Directory, use dir() instead: ``Directory`` is the object returned by the ``dir()`` function, giving access to ``path``, ``handle``, ``read()``, ``rewind()`` and ``close()``
:twitter:creator: @exakat
:twitter:image:src: https://php-errors.readthedocs.io/en/latest/_static/logo.png
.. raw:: html
Description
___________
``Directory`` is the object returned by the ``dir()`` function, giving access to ``path``, ``handle``, ``read()``, ``rewind()`` and ``close()``. It predates the rest of PHP's object model and is a thin, internal wrapper around a directory stream: it has no public constructor of its own.
Calling ``new Directory(...)`` directly is rejected, because the object needs to be initialized with an already-opened directory handle, something only ``dir()`` can set up correctly.
Example
_______
.. code-block:: php
Solutions
_________
+ Use ``dir($path)`` to obtain a ``Directory`` object.
+ Use ``opendir()``, ``readdir()``, ``closedir()`` for a purely procedural approach instead of the ``Directory`` object.
+ Use ``DirectoryIterator`` or ``RecursiveDirectoryIterator`` for an object-oriented, iterable interface.
Related Error Messages
______________________
+ :ref:`sqlite3result-cannot-be-directly-instantiated`
+ :ref:`the-sqlite3-object-has-not-been-correctly-initialised-or-is-already-closed`
See Also
________
+ `dir `_
+ `Directory `_
Changed Behavior
________________
This error may appear following an evolution in behavior, in previous versions. See ` `_.