1
- # PHP RFC: Change Directory class to behave like an opaque object
1
+ # PHP RFC: Change Directory class to behave like a resource object
2
2
3
3
- Version: 0.1
4
4
- Date: 2024-09-14
10
10
11
11
## Introduction
12
12
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,
15
16
which in general implies, being ` final ` , being not serializable,
16
17
not constructible via ` new ` , cannot be cast, and to not implement any methods.
17
18
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()`,
21
22
which is visible if one tries to call one of its methods.
22
23
23
24
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.
25
26
26
27
## Proposal
27
28
@@ -33,6 +34,33 @@ We propose to make the following changes to the `Directory` class:
33
34
- Ban serialization of it via the ` @not-serializable ` doc comment on the class stub
34
35
- Ban creating dynamic properties on an instance of ` Directory ` via the ` @strict-properties ` doc comment on the class stub
35
36
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
+
36
64
## Backward Incompatible Changes
37
65
38
66
It will no longer be possible:
@@ -51,6 +79,8 @@ VOTING_SNIPPET
51
79
52
80
## Future scope
53
81
82
+ - Add support to initialize the class via ` new `
83
+ - Add support for cloning
54
84
55
85
## References
56
86
0 commit comments