The Directory class is what you get back from dir(). This RFC from Gina Peter Banyard makes it behave like a "resource object", a class that wraps an internal PHP handle and can't be constructed, cloned or modified by userland code.
Today you can write new Directory() and get an object that looks fine but breaks as soon as you call one of its methods. The only way to get a working one is through dir(). The RFC also notes that PHP will likely reuse this class when directory resources are converted to objects.
What it changes
The Directory class gets five changes:
- It becomes
final, so you can't extend it. new Directory()throws anError.- It can't be cloned.
- It can't be serialized.
- You can't add dynamic properties to it.
$dir = dir('/tmp'); // still works new Directory(); // now throws an Error
The RFC gives a reason for each change. The object wraps an internal stream, and there's no way to duplicate a stream, so cloning can't work. Serializing the state of a file system handle makes no sense. And a dynamic property on this object is almost certainly a bug.
What it means for existing code
These things stop working:
- Extending the
Directoryclass - Cloning, serializing or adding dynamic properties to a
Directory - Creating one with
new
If you only get Directory objects from dir() and use them as they are, nothing changes for you. The RFC lists allowing new and cloning as possible future improvements.
The vote
Accepted. Voting closed on October 20, 2024 with 23 in favor and 0 against, above the two-thirds majority it needed. It shipped in PHP 8.5.