Cannot directly construct Directory, use dir() instead

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

<?php

$d = new Directory('.');

?>

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.

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>`_.