Skip to content

Commit c7155c3

Browse files
committed
added interface documentation
1 parent f14b093 commit c7155c3

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

drafts/records.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,46 @@ record User(string $name, string $emailAddress) {
289289
}
290290
```
291291

292+
### Implementing Interfaces
293+
294+
A **record** can implement interfaces, but it cannot extend other records or classes, but may use traits:
295+
296+
```php
297+
interface Vehicle {}
298+
299+
interface Car extends Vehicle {
300+
public function drive(): void;
301+
}
302+
303+
interface SpaceShip extends Vehicle {
304+
public function launch(): void;
305+
}
306+
307+
record FancyCar(string $model) implements Car {
308+
public function drive(): void {
309+
echo "Driving a Fancy Car {$this->model}";
310+
}
311+
}
312+
313+
record SpaceCar(string $model) implements Car, SpaceShip {
314+
public function drive(): void {
315+
echo "Driving a Space Car {$this->model}";
316+
}
317+
318+
public function launch(): void {
319+
echo "Launching a Space Car {$this->model}";
320+
}
321+
}
322+
323+
record Submarine(string $model) implements Vehicle {
324+
use Submersible;
325+
}
326+
327+
record TowTruct(string $model, private Car $towing) implements Car {
328+
use Towable;
329+
}
330+
```
331+
292332
### Mental models and how it works
293333

294334
From the perspective of a developer, declaring a record declares an object with the same name.

published/records.ptxt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,46 @@ record User(string $name, string $emailAddress) {
245245
}
246246
</code>
247247

248+
==== Implementing Interfaces ====
249+
250+
A **record** can implement interfaces, but it cannot extend other records or classes, but may use traits:
251+
252+
<code php>
253+
interface Vehicle {}
254+
255+
interface Car extends Vehicle {
256+
public function drive(): void;
257+
}
258+
259+
interface SpaceShip extends Vehicle {
260+
public function launch(): void;
261+
}
262+
263+
record FancyCar(string $model) implements Car {
264+
public function drive(): void {
265+
echo "Driving a Fancy Car {$this->model}";
266+
}
267+
}
268+
269+
record SpaceCar(string $model) implements Car, SpaceShip {
270+
public function drive(): void {
271+
echo "Driving a Space Car {$this->model}";
272+
}
273+
274+
public function launch(): void {
275+
echo "Launching a Space Car {$this->model}";
276+
}
277+
}
278+
279+
record Submarine(string $model) implements Vehicle {
280+
use Submersible;
281+
}
282+
283+
record TowTruct(string $model, private Car $towing) implements Car {
284+
use Towable;
285+
}
286+
</code>
287+
248288
==== Mental models and how it works ====
249289

250290
From the perspective of a developer, declaring a record declares an object with the same name. The developer can consider the record function (the inline constructor) as a factory function that creates a new object or retrieves an existing object from an array.

0 commit comments

Comments
 (0)