[php-src] Issue #19026: Proposal: Native async/await Support in PHP
| From: | pkfan | Date: | Thu, 03 Jul 2025 17:58:24 +0000 |
| Subject: | [php-src] Issue #19026: Proposal: Native async/await Support in PHP | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-250374@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/19026
Author: pkfan
### 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**
1. **Alignment with Modern Languages**:
- JavaScript’s async/await (introduced in ES2017) simplified callback
hell into linear, readable code. For example:
```javascript
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:
```python
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 .
2. **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 .
3. **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 or exec() .
### **Proposed Syntax**
```php
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’s Promise or Python’s Coroutine).
- **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**
1. Discuss feasibility and syntax details on the mailing list.
2. Reference prior art:
- JavaScript’s [Async
Functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)
- Python’s [asyncio](https://docs.python.org/3/library/asyncio.html)
- PHP’s [Fibers RFC](https://wiki.php.net/rfc/fibers) and [True Async RFC](#) .
This would position PHP as a competitive choice for modern, high-performance applications. Thank you
for considering!