Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Commit 6e5252e

Browse files
committed
Clarify usage of spawn and await keywords in PHP, including naming restrictions and placement rules
1 parent 3e7e7da commit 6e5252e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

basic.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,25 @@ spawn "sleep"(5);
503503
spawn (fn() => sleep(1))();
504504
```
505505

506+
The `spawn` or `with` keywords does not affect the ability to define `functions`, `constants`, or `classes`
507+
with the same name:
508+
509+
```php
510+
function spawn(): void {} // <- Allowed
511+
512+
const SPAWN = 1; // <- Allowed
513+
514+
class Spawn {} // <- Allowed
515+
```
516+
517+
The `spawn` expression cannot appear before `namespace` or `use` declarations:
518+
519+
```php
520+
spawn myFunction(); // <- Not allowed
521+
namespace MyNamespace;
522+
use MyNamespace\MyClass;
523+
```
524+
506525
#### Spawn closure expression
507526

508527
Allows creating a coroutine from a closure directly when using `spawn`:
@@ -1103,6 +1122,25 @@ The `await` expression can be used just like any other expression together with
11031122
}
11041123
```
11051124

1125+
The `await` or `until` keywords does not affect the ability to define `functions`, `constants`, or `classes`
1126+
with the same name:
1127+
1128+
```php
1129+
function await(): void {} // <- Allowed
1130+
1131+
const AWAIT = 1; // <- Allowed
1132+
$x = AWAIT + 2; // <- Allowed
1133+
1134+
class Await {} // <- Allowed
1135+
```
1136+
1137+
The `await` expression cannot appear before `namespace` or `use` declarations:
1138+
1139+
```php
1140+
await file_get_contents('file1.txt'); // <- Not allowed
1141+
namespace MyNamespace;
1142+
use MyNamespace\MyClass;
1143+
```
11061144

11071145
#### Await with cancellation
11081146

0 commit comments

Comments
 (0)