{
    "slug": "php_async_frameworks",
    "term": "PHP Async Frameworks",
    "category": "performance",
    "difficulty": "advanced",
    "short": "ReactPHP and Amp provide event-loop async I/O — enabling concurrent HTTP requests without blocking.",
    "long": "ReactPHP: event-driven, non-blocking I/O built on PHP streams. Amp v3: coroutine-based using PHP 8.1 Fibers. Both enable concurrent HTTP requests to multiple APIs simultaneously. Use where a service makes many external API calls — sequential calls taking N*latency become ~1*latency with concurrency.",
    "aliases": [
        "ReactPHP",
        "Amp PHP",
        "Revolt",
        "async PHP"
    ],
    "tags": [
        "performance",
        "php",
        "async"
    ],
    "misconception": "ReactPHP replaces PHP-FPM for web apps — for typical web apps FPM is simpler; async shines for services making many concurrent I/O calls.",
    "why_it_matters": "PHP's traditional request-per-process model means concurrency is achieved through horizontal scaling — more server processes. Async frameworks like Swoole, ReactPHP, and RoadRunner change this by keeping a persistent event loop that handles many concurrent I/O operations in a single process. The performance gains are real for I/O-bound workloads but the programming model is fundamentally different — blocking calls stall the entire event loop, and PHP libraries written for synchronous execution often cannot be used directly without wrappers.",
    "common_mistakes": [
        "Blocking I/O inside async loop",
        "Using sync PDO in async context",
        "Not handling promise rejection",
        "Async for sequential workflows"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_concurrency_options",
        "fibers",
        "swoole_openswoole"
    ],
    "prerequisites": [
        "fibers",
        "php_concurrency_options",
        "coroutines"
    ],
    "refs": [
        "https://reactphp.org/"
    ],
    "bad_code": "foreach ($apis as $url) { $results[] = file_get_contents($url); } // 10s serial",
    "good_code": "$browser = new Browser();\n$promises = array_map(fn($u) => $browser->get($u), $apis);\n\\React\\Promise\\all($promises)->then(fn($r) => process($r));\nLoop::run(); // ~1s",
    "quick_fix": "Choose ReactPHP for legacy code (EventEmitter-based, large ecosystem) or Amp v3 for modern PHP 8.1+ Fiber-based async/await syntax — both use the Revolt event loop",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-16",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_async_frameworks",
        "html_url": "https://codeclaritylab.com/glossary/php_async_frameworks",
        "json_url": "https://codeclaritylab.com/glossary/php_async_frameworks.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[PHP Async Frameworks](https://codeclaritylab.com/glossary/php_async_frameworks) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_async_frameworks"
            }
        }
    }
}