You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: published/records.ptxt
+14-14Lines changed: 14 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ $uid = $user->id;
26
26
// ...
27
27
$uid = 5; // somehow accidentally sets uid to an unrelated integer
28
28
// ...
29
-
updateUserRole($uid, Role::ADMIN()); // accidental passing of
29
+
updateUserRole($uid, Role::ADMIN()); // accidental passing of
30
30
</code>
31
31
32
32
In this example, the uid is accidentally set to a plain integer, and updateUserRole is called with the wrong value.
@@ -58,9 +58,9 @@ updateUserRole($uid, Role::ADMIN()); // This will throw an error
58
58
59
59
This RFC proposes the introduction of a new record keyword in PHP to define immutable data objects. These objects will allow properties to be initialized concisely and will provide built-in methods for common operations such as modifying properties and equality checks using a function-like instantiation syntax. Records can implement interfaces and use traits but cannot extend other records or classes; composition is allowed, however.
60
60
61
-
=== Syntax and semantics ===
61
+
==== Syntax and semantics ====
62
62
63
-
== Definition ==
63
+
=== Definition ===
64
64
65
65
A ''%%record%%'' is defined by the word "record", followed by the name of its type, an open parenthesis containing one or more typed parameters that become public, immutable, properties. They may optionally implement an interface using the ''%%implements%%'' keyword. A ''%%record%%'' body is optional.
66
66
@@ -89,18 +89,18 @@ record PaintBucket(StockPaint ...$constituents) {
89
89
public function mixIn(StockPaint $paint): PaintBucket {
A ''%%record%%'' may be used as a ''%%readonly class%%'', as the behavior of it is very similar with no key differences to assist in migration from ''%%readonly class%%''.
102
102
103
-
== Optional parameters and default values ==
103
+
=== Optional parameters and default values ===
104
104
105
105
A ''%%record%%'' can also be defined with optional parameters that are set if left out during instantiation.
106
106
@@ -109,7 +109,7 @@ record Rectangle(int $x, int $y = 10);
109
109
var_dump(Rectangle(10)); // output a record with x: 10 and y: 10
110
110
</code>
111
111
112
-
== Auto-generated with method ==
112
+
=== Auto-generated with method ===
113
113
114
114
To enhance the usability of records, the RFC proposes automatically generating a ''%%with%%'' method for each record. This method allows for partial updates of properties, creating a new instance of the record with the specified properties updated.
$mickey = $pluto->with(name: "Mickey"); // no named argument for population error
140
140
</code>
141
141
142
-
== Constructors ==
142
+
=== Constructors ===
143
143
144
144
Optionally, they may also define a constructor to provide validation or other initialization logic:
145
145
@@ -151,7 +151,7 @@ record User(string $name, string $email) {
151
151
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
152
152
throw new InvalidArgumentException("Invalid email address");
153
153
}
154
-
154
+
155
155
$this->id = hash('sha256', $email);
156
156
$this->name = ucwords($name);
157
157
}
@@ -160,7 +160,7 @@ record User(string $name, string $email) {
160
160
161
161
During construction, a ''%%record%%'' is fully mutable. This allows the developer freedom to mutate properties as needed to ensure a canonical representation of an object.
162
162
163
-
=== Performance considerations ===
163
+
==== Performance considerations ====
164
164
165
165
To ensure that records are both performant and memory-efficient, the RFC proposes leveraging PHP's copy-on-write (COW) semantics (similar to arrays) and interning values. Unlike interned strings, the garbage collector will be allowed to clean up these interned records when they are no longer needed.
166
166
@@ -172,13 +172,13 @@ $point3 = Point(3, 4); // No data duplication here either, it is pointing the th
172
172
$point4 = $point1->with(x: 5); // Data duplication occurs here, creating a new instance with modified data
173
173
</code>
174
174
175
-
== Cloning and with() ==
175
+
=== Cloning and with() ===
176
176
177
177
Calling ''%%clone%%'' on a ''%%record%%'' results in the exact same record object being returned. As it is a "value" object, it represents a value and is the same thing as saying ''%%clone 3%%''—you expect to get back a ''%%3%%''.
178
178
179
179
''%%with%%'' may be called with no arguments, and it is the same behavior as ''%%clone%%''. This is an important consideration because a developer may call ''%%$new = $record->with(...$array)%%'' and we don’t want to crash. If a developer wants to crash, they can do by ''%%assert($new !== $record)%%''.
180
180
181
-
=== Equality ===
181
+
==== Equality ====
182
182
183
183
A ''%%record%%'' is always strongly equal (''%%===%%'') to another record with the same value in the properties, much like an ''%%array%%'' is strongly equal to another array containing the same elements. For all intents, ''%%$recordA === $recordB%%'' is the same as ''%%$recordA == $recordB%%''.
184
184
@@ -232,11 +232,11 @@ foreach ($constructor->getParameters() as $param) {
232
232
* A new function, ''%%is_record($record)%%'', will return ''%%true%%'' for records, and ''%%false%%'' otherwise
233
233
* Calling ''%%get_class($record)%%'' will return the record name
234
234
235
-
=== var_dump ===
235
+
==== var_dump ====
236
236
237
237
Calling ''%%var_dump%%'' will look much like it does for objects, but instead of ''%%object%%'' it will say ''%%record%%''.
0 commit comments