-
Notifications
You must be signed in to change notification settings - Fork 8k
Expand file tree
/
Copy pathgeneric_abstract_class.phpt
More file actions
61 lines (50 loc) · 1.33 KB
/
generic_abstract_class.phpt
File metadata and controls
61 lines (50 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
--TEST--
Generic class: abstract generic class with abstract methods
--FILE--
<?php
declare(strict_types=1);
abstract class Repository<T> {
abstract public function find(int $id): T;
abstract public function save(T $entity): void;
public function findAndSave(int $id): T {
$item = $this->find($id);
$this->save($item);
return $item;
}
}
class User {
public function __construct(public int $id, public string $name) {}
}
class UserRepository extends Repository<User> {
private array $store = [];
public function find(int $id): User {
return $this->store[$id] ?? new User($id, "User$id");
}
public function save(User $entity): void {
$this->store[$entity->id] = $entity;
}
}
// 1. Basic usage
$repo = new UserRepository();
$user = $repo->find(1);
echo "1. " . $user->name . "\n";
// 2. Save and retrieve
$repo->save(new User(2, "Alice"));
echo "2. " . $repo->find(2)->name . "\n";
// 3. Template method pattern
$u = $repo->findAndSave(3);
echo "3. " . $u->name . "\n";
// 4. Cannot instantiate abstract generic class
try {
$r = new Repository<User>();
} catch (Error $e) {
echo "4. Error: " . (str_contains($e->getMessage(), 'abstract') ? 'abstract' : $e->getMessage()) . "\n";
}
echo "Done.\n";
?>
--EXPECT--
1. User1
2. Alice
3. User3
4. Error: abstract
Done.