Skip to content

Commit 282c211

Browse files
committed
add intersection type section
1 parent 6dd6ad3 commit 282c211

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

translations/es-AR.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,50 @@ Class Foo()
280280
}
281281
```
282282

283+
#### Tipo de intersección
284+
285+
![php-version-81](https://shields.io/badge/php->=8.1-blue)
286+
287+
Desde PHP 8.1, podés usar un "tipo de intersección" (también conocido como "puro") que exige que un valor dado pertenezca a todos los tipos. Por ejemplo, este parámetro necesita implementar las interfaces *Stringable* y *Countable*:
288+
289+
```php
290+
function myFunction(Stringable&Countable $param): Stringable&Countable
291+
{
292+
return $param;
293+
}
294+
Class Foo
295+
{
296+
public function __toString() {
297+
return "something";
298+
}
299+
}
300+
myFunction(new Foo());
301+
// TypeError: myFunction(): Argument #1 ($param) must be of type Stringable&Countable, Foo given
302+
```
303+
304+
También funciona con propiedades de clase:
305+
306+
```php
307+
Class Foo
308+
{
309+
public Stringable&Countable $bar;
310+
}
311+
```
312+
313+
El tipo de intersección solo admite clases e interfaces. Los tipos escalares (string, int, array, null, mixed, etc.) no están permitidos:
314+
315+
```php
316+
function myFunction(string&Countable $param)
317+
{
318+
return $param;
319+
}
320+
// PHP Fatal error: Type string cannot be part of an intersection type
321+
```
322+
323+
##### Recurso externo
324+
325+
- [Intersection types on PHP.Watch](https://php.watch/versions/8.1/intersection-types)
326+
283327
#### Tipo Nullable
284328

285329
![php-version-71](https://shields.io/badge/php->=7.1-blue)

0 commit comments

Comments
 (0)