Closed as duplicate of#18974
Description
Description
Dear PHP Internals Team,
I’d like to propose adding native async
/await
syntax to PHP, similar to implementations in JavaScript (ES2017+) and Python (3.5+). As modern applications increasingly rely on non-blocking I/O for scalability (e.g., APIs, microservices, real-time systems), PHP’s current reliance on third-party solutions like Swoole, ReactPHP, or AMPHP fragments the ecosystem and complicates maintenance .
Why This Matters
-
Alignment with Modern Languages:
- JavaScript’s
async
/await
(introduced in ES2017) simplified callback hell into linear, readable code. For example:async function fetchData() { const response = await fetch('https://api.example.com'); return response.json(); }
- Python’s
async
/await
(since 3.5) enabled seamless coroutine integration:async def fetch_data(): async with aiohttp.ClientSession() as session: response = await session.get('https://api.example.com') return await response.json()
Both languages saw widespread adoption due to developer ergonomics and performance gains .
- JavaScript’s
-
PHP’s Current Limitations:
- While Fibers (PHP 8.1+) provide low-level concurrency primitives, they lack syntactic sugar, forcing developers to manage suspension/resume manually . Libraries like AMPHP require verbose boilerplate (e.g.,
yield
-based coroutines) compared to native keywords . - RFCs like True Async (under discussion) highlight community demand for standardized coroutines and cancellation APIs .
- While Fibers (PHP 8.1+) provide low-level concurrency primitives, they lack syntactic sugar, forcing developers to manage suspension/resume manually . Libraries like AMPHP require verbose boilerplate (e.g.,
-
Performance and Scalability:
- Non-blocking I/O can reduce latency for HTTP requests, database queries, and file operations by up to 80% (e.g., Guzzle Async vs. synchronous requests ). Native support would eliminate overhead from userland workarounds like
pcntl_fork
orexec()
.
- Non-blocking I/O can reduce latency for HTTP requests, database queries, and file operations by up to 80% (e.g., Guzzle Async vs. synchronous requests ). Native support would eliminate overhead from userland workarounds like
Proposed Syntax
async function fetchData(): Awaitable<string> {
$response = await $httpClient->getAsync('https://api.example.com');
return $response->getBody();
}
Key features to consider:
Awaitable
Type: A return type hint for async functions (analogous to JavaScript’sPromise
or Python’sCoroutine
).- Structured Concurrency: Integrate with RFC proposals like scoped coroutines and cancellation tokens .
- Backward Compatibility: Allow gradual adoption by interoperating with Fibers and existing event loops (e.g., ReactPHP) .
Next Steps
- Discuss feasibility and syntax details on the mailing list.
- Reference prior art:
- JavaScript’s Async Functions
- Python’s asyncio
- PHP’s Fibers RFC and True Async RFC .
This would position PHP as a competitive choice for modern, high-performance applications. Thank you for considering!