{
    "slug": "php8_fibers_intro",
    "term": "Fibers — Cooperative Concurrency (PHP 8.1)",
    "category": "php",
    "difficulty": "advanced",
    "short": "PHP 8.1 Fibers enable cooperative multitasking — suspending execution at yield points and resuming later — the foundation for async PHP frameworks without OS threads.",
    "long": "Fiber::create($callback) creates a fiber. $fiber->start($args) begins execution. Fiber::suspend($value) pauses and returns value to caller. $fiber->resume($value) continues from the suspension point. Fibers are single-threaded — not parallel. They enable: event-loop integration (ReactPHP, Revolt, Swoole), cooperative multitasking, coroutine-style async code without callbacks. The Revolt event loop uses Fibers to implement async/await-like patterns. Unlike generators, Fibers can suspend from any call depth (generators only yield from the generator function itself). This is the PHP equivalent of Python's asyncio coroutines.",
    "aliases": [],
    "tags": [
        "php",
        "php81",
        "fibers",
        "async",
        "history"
    ],
    "misconception": "Fibers provide parallelism — they're cooperative concurrency (single-threaded). Multiple fibers take turns, not run simultaneously. Use pcntl_fork or worker_threads for actual parallelism.",
    "why_it_matters": "Fibers enable proper async programming in PHP without callback hell — the foundation for the next generation of PHP async frameworks and database drivers.",
    "common_mistakes": [
        "Expecting Fibers to use multiple CPU cores — they're single-threaded.",
        "Using Fibers without an event loop — fibers alone don't help without something to schedule them.",
        "Confusing with PHP generators — fibers can suspend from any call depth."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php5_generators_intro",
        "async_processing",
        "php_pcntl_signals",
        "oauth2"
    ],
    "prerequisites": [
        "php5_generators_intro",
        "async_processing"
    ],
    "refs": [
        "https://www.php.net/manual/en/class.fiber.php"
    ],
    "bad_code": "// Generators only yield from generator function:\nfunction gen() {\n    yield doSomething(); // Only works at generator level\n}",
    "good_code": "$fiber = new Fiber(function(): void {\n    $value = Fiber::suspend('first');\n    echo 'Resumed with: ' . $value . PHP_EOL;\n});\n\n$yielded = $fiber->start(); // 'first'\n$fiber->resume('hello'); // 'Resumed with: hello'\n\n// Real use: Revolt event loop\n// use Revolt\\EventLoop;\n// EventLoop::run();",
    "quick_fix": "Use Fibers via Revolt/ReactPHP event loop rather than directly. Revolt's async/await API wraps Fibers in a usable interface.",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php8_fibers_intro",
        "html_url": "https://codeclaritylab.com/glossary/php8_fibers_intro",
        "json_url": "https://codeclaritylab.com/glossary/php8_fibers_intro.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": "[Fibers — Cooperative Concurrency (PHP 8.1)](https://codeclaritylab.com/glossary/php8_fibers_intro) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php8_fibers_intro"
            }
        }
    }
}