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
{{ message }}
Repository navigation
Commit 2b7702b
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/generics/basics-and-bounds.md
+73-12Lines changed: 73 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,68 @@ TypePHP manages generic templates at two distinct execution levels:
38
38
39
39
---
40
40
41
+
## Deep Enforcement vs. Boundary Enforcement (Included vs. Excluded Classes)
42
+
43
+
It is crucial to understand **when** TypePHP intercepts a generic type violation. This depends entirely on whether the generic class (e.g., `Collection`) is **included** or **excluded** in your `typephp.php` configuration.
44
+
45
+
### 1. Included Classes (Deep Enforcement)
46
+
If you want TypePHP to throw an error the exact millisecond you call an internal method like `$collection->add(new Product())` or `new Collection([1, 'invalid'])`, **the generic class itself must be whitelisted in your `typephp.php``include` array.**
47
+
48
+
When a class is included, TypePHP intercepts every method call and constructor inside it.
49
+
50
+
```php
51
+
// 1. You explicitly included your custom collection:
// "Argument $item (template TValue) must be of type User, Product given"
59
+
$users->add(new Product());
60
+
```
61
+
62
+
### 2. Excluded Classes (Boundary Enforcement)
63
+
Most third-party generic containers (like Laravel's `Illuminate\Support\Collection` or Doctrine's `ArrayCollection`) live in `vendor/**`, which is usually **excluded** for maximum framework performance.
64
+
65
+
When a generic class is excluded, TypePHP **does not intercept its internal constructors or methods, and does not inspect its internal properties**. Instead, it tracks the object's generic "badge" in memory and strictly enforces the contract **by comparing badges** when the object crosses into an included function or method.
$strings = new Collection(['valid_string', 12345]);
74
+
75
+
// NO ERROR THROWN YET!
76
+
// Because Collection.php is excluded, TypePHP does not intercept the constructor
77
+
// to check the raw array contents. It simply attaches the `string` badge to the object.
78
+
79
+
// 3. BOUNDARY GATEKEEPER CATCHES TYPE CONFLICTS
80
+
class NumberService {
81
+
/** @param Collection<int,int> $numbers */
82
+
public function process(Collection $numbers) {}
83
+
}
84
+
85
+
$service = new NumberService();
86
+
87
+
// 💥 Throws TypeError at the function boundary!
88
+
// "Argument $numbers expects Collection<covariantint>, but Collection<string> was given"
89
+
$service->process($strings);
90
+
```
91
+
92
+
**Why this happens:** TypePHP doesn't open the sealed "Vendor Box" to look at the `12345` or `'valid_string'` inside it. Instead, the `NumberService` acts as a gatekeeper:
93
+
**"I require a box labeled `int`."*
94
+
**"You handed me a box labeled `string`."*
95
+
***Access Denied.**
96
+
97
+
### Which should you choose?
98
+
***Exclude generic framework classes (`vendor/**`)** to keep the framework booting at 100% native C-speed. TypePHP will still perfectly guard your application code (`app/**`) by ensuring no generic containers with conflicting type badges ever cross into your services or controllers.
99
+
***Include specific generic classes** if you own them (e.g. `app/Data/Result.php`), or if you specifically want deep, immediate validation on every single method call and constructor argument within that container.
100
+
101
+
---
102
+
41
103
## PHP Limitation: No Native `instanceof` on Generics
42
104
43
105
In PHP, writing generic syntax directly in executable statements (such as `if ($obj instanceof Collection<User>)`) is a syntax error.
@@ -192,35 +254,35 @@ Generic parameters (`T`) in TypePHP are not limited to object classes. You can b
192
254
### Generic Collections of Refined Scalars (`Collection<positive-int>`)
193
255
194
256
```php
195
-
/** @var Collection<positive-int> $scores */
196
-
$scores = new Collection();
257
+
/** @var MyCollection<positive-int> $scores */
258
+
$scores = new MyCollection();
197
259
198
260
$scores->add(100); // Valid
199
261
200
262
$scores->add(-50);
201
-
// Throws: TypeError: Collection::add(): Argument $item (template T = positive-int) must be of type positive-int
263
+
// Throws: TypeError: MyCollection::add(): Argument $item (template T = positive-int) must be of type positive-int
202
264
```
203
265
204
266
### Generic Collections of Array Shapes (`Collection<array{...}>`)
// Throws: TypeError: Collection::add(): Argument $item['id'] must be of type positive-int
275
+
// Throws: TypeError: MyCollection::add(): Argument $item['id'] must be of type positive-int
214
276
```
215
277
216
278
---
217
279
218
280
## First-Use Type Inference (Unannotated Generic Instances)
219
281
220
-
If you instantiate a generic class without an inline `@var` prebinding annotation:
282
+
If you instantiate an **included** generic class without an inline `@var` prebinding annotation:
221
283
222
284
```php
223
-
$collection = new Collection(); // No @var Collection<User> annotation!
285
+
$collection = new MyCollection(); // No @var MyCollection<User> annotation!
224
286
```
225
287
226
288
TypePHP automatically infers the template parameter `T` from the **first method call** executed on that object instance and locks `T` to that type in `WeakMap` memory for all subsequent calls:
0 commit comments