Skip to content

Commit 65605b8

Browse files
committed
add sub section for new intersection type
1 parent 88cdd2c commit 65605b8

File tree

1 file changed

+49
-5
lines changed

1 file changed

+49
-5
lines changed

README.md

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ $a = myFunction();
232232
You can set a type to a class property:
233233

234234
```php
235-
Class Foo()
235+
Class Foo
236236
{
237237
public int $bar;
238238
}
@@ -244,7 +244,7 @@ $f->bar = 'baz'; // TypeError: Cannot assign string to property Foo::$bar of typ
244244

245245
![php-version-80](https://shields.io/badge/php->=8.0-blue)
246246

247-
You can use a “union type” that accepts values of multiple different types, rather than a single one:
247+
Since PHP 8.0, you can use a “union type” that accepts values of multiple different types, rather than a single one:
248248

249249
```php
250250
function myFunction(string|int|array $param): string|int|array
@@ -256,12 +256,56 @@ function myFunction(string|int|array $param): string|int|array
256256
It also works with class property:
257257

258258
```php
259-
Class Foo()
259+
Class Foo
260260
{
261261
public string|int|array $bar;
262262
}
263263
```
264264

265+
#### Intersection type
266+
267+
![php-version-81](https://shields.io/badge/php->=8.1-blue)
268+
269+
Since PHP 8.1, you can use an "intersection type" (also known as "pure") that enforce that a given value belong to every types. For example this param needs to implement both __Stringable__ and __Countable__ interfaces:
270+
271+
```php
272+
function myFunction(Stringable&Countable $param): Stringable&Countable
273+
{
274+
return $param;
275+
}
276+
Class Foo
277+
{
278+
public function __toString() {
279+
return "something";
280+
}
281+
}
282+
myFunction(new Foo());
283+
// TypeError: myFunction(): Argument #1 ($param) must be of type Stringable&Countable, Foo given
284+
```
285+
286+
It also works with class property:
287+
288+
```php
289+
Class Foo
290+
{
291+
public Stringable&Countable $bar;
292+
}
293+
```
294+
295+
Intersection type only supports class and interfaces. Scalar types (string, int, array, null, mixed, etc) are not allowed:
296+
297+
```php
298+
function myFunction(string&Countable $param)
299+
{
300+
return $param;
301+
}
302+
// PHP Fatal error: Type string cannot be part of an intersection type
303+
```
304+
305+
##### External resource
306+
307+
- [Intersection types on PHP.Watch](https://php.watch/versions/8.1/intersection-types)
308+
265309
#### Nullable type
266310

267311
![php-version-71](https://shields.io/badge/php->=7.1-blue)
@@ -356,7 +400,7 @@ function myFunction(): void|null
356400
You can set a nullable type to a class property:
357401

358402
```php
359-
Class Foo()
403+
Class Foo
360404
{
361405
public int|null $bar;
362406
}
@@ -1166,7 +1210,7 @@ $a = concat(first: 'foo', second: 'bar', third: 'baz');
11661210
Named arguments also work with object constructor:
11671211

11681212
```php
1169-
Class Foo()
1213+
Class Foo
11701214
{
11711215
public function __construct(
11721216
public string $first,

0 commit comments

Comments
 (0)