Skip to content

Proposal: Native async/await Support in PHP #19026

Closed as duplicate of#18974
Closed as duplicate of#18974
@pkfan

Description

@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:
      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 .

  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

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:

This would position PHP as a competitive choice for modern, high-performance applications. Thank you for considering!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions