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 aDirectoryobject.Use
opendir(),readdir(),closedir()for a purely procedural approach instead of theDirectoryobject.Use
DirectoryIteratororRecursiveDirectoryIteratorfor 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>`_.