Skip to content

Commit 10c4ff2

Browse files
committed
Add some details for Directory RFC
1 parent c91539d commit 10c4ff2

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

accepted/directory-opaque-object.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PHP RFC: Change Directory class to behave like an opaque object
1+
# PHP RFC: Change Directory class to behave like a resource object
22

33
- Version: 0.1
44
- Date: 2024-09-14
@@ -10,8 +10,9 @@
1010

1111
## Introduction
1212

13-
The `Directory` class is probably the first instance of what we now call an "opaque object".
14-
Opaque objects are normally the result of converting resources to objects,
13+
The `Directory` class is probably the first instance of what we now call a "resource object"
14+
(and in its stricter sense an "opaque object").
15+
Resource/Opaque objects are usually the result of converting resources to objects,
1516
which in general implies, being `final`, being not serializable,
1617
not constructible via `new`, cannot be cast, and to not implement any methods.
1718
However, as this class has existed since PHP 4 none of these things are formally implemented.
@@ -21,7 +22,7 @@ But one can create a broken instance by just using `new Directory()`,
2122
which is visible if one tries to call one of its methods.
2223

2324
As it seems likely that we will repurpose this class when converting directory resources to objects;
24-
we think it makes sense to already convert this class to behave like an opaque object.
25+
we think it makes sense to already convert this class to behave like a resource object.
2526

2627
## Proposal
2728

@@ -33,6 +34,33 @@ We propose to make the following changes to the `Directory` class:
3334
- Ban serialization of it via the `@not-serializable` doc comment on the class stub
3435
- Ban creating dynamic properties on an instance of `Directory` via the `@strict-properties` doc comment on the class stub
3536

37+
## Rationales
38+
### Preventing initialization via new
39+
40+
The stream layer of PHP emits warnings and may result in uninitialized streams.
41+
Constructors must always either throw an exception, or create a valid object.
42+
As these semantics are not straightforward to implement when creating streams we continue to rely on `dir()`
43+
to create instances of this class as it does not have the above constraints.
44+
45+
### Making the class final
46+
47+
As this class is a wrapper around an internal stream resource,
48+
and cannot be properly initialized without it being returned by `dir()`.
49+
Extending it doesn't make any sense.
50+
51+
### Preventing cloning
52+
53+
As this class is a wrapper around an internal stream resource,
54+
and there is no capability to duplicate streams, there is no reasonable way to implement cloning.
55+
56+
### Preventing serialization
57+
58+
Trying to serialize (and unserialize) the state of a given file system doesn't make any sense.
59+
60+
### Preventing the creation of dynamic properties
61+
62+
Creating a dynamic property on an instance of this class points to a definite bug.
63+
3664
## Backward Incompatible Changes
3765

3866
It will no longer be possible:
@@ -51,6 +79,8 @@ VOTING_SNIPPET
5179

5280
## Future scope
5381

82+
- Add support to initialize the class via `new`
83+
- Add support for cloning
5484

5585
## References
5686

0 commit comments

Comments
 (0)