{
    "slug": "spl_data_structures",
    "term": "SPL Data Structures",
    "category": "php",
    "difficulty": "advanced",
    "short": "PHP's Standard PHP Library provides efficient built-in data structures: SplStack, SplQueue, SplHeap, SplMinHeap, SplDoublyLinkedList, and more.",
    "long": "The SPL (Standard PHP Library) data structures offer typed, memory-efficient alternatives to plain arrays for specific use cases. SplStack (LIFO), SplQueue (FIFO), and SplDoublyLinkedList avoid the overhead of shifting/reindexing arrays. SplMinHeap and SplMaxHeap implement priority queues in O(log n). SplFixedArray is a memory-efficient fixed-size integer-indexed array. SplObjectStorage maps objects to data without string keys. In practice, PHP arrays cover most needs, but SPL structures shine in algorithms, job schedulers, and data processing pipelines where their complexity guarantees matter.",
    "aliases": [
        "PHP SPL",
        "SplStack",
        "SplQueue",
        "SplDoublyLinkedList"
    ],
    "tags": [
        "php",
        "spl",
        "data-structures",
        "performance"
    ],
    "misconception": "PHP arrays are always the best choice for stacks and queues. SplStack and SplQueue provide O(1) push/pop from both ends and use less memory than PHP arrays for large collections — arrays are flexible but SPL structures are more efficient when the access pattern is known.",
    "why_it_matters": "PHP's SPL provides typed data structures (SplStack, SplQueue, SplHeap, SplFixedArray) that are more memory-efficient and semantically correct than misusing plain PHP arrays for those purposes.",
    "common_mistakes": [
        "Using a PHP array as a stack with array_push/array_pop when SplStack expresses the intent explicitly.",
        "Not using SplFixedArray for large numeric arrays — it uses significantly less memory than a dynamic array.",
        "Using SplDoublyLinkedList when SplStack or SplQueue (which extend it) communicate intent better.",
        "Not considering that SplMinHeap/SplMaxHeap provide O(log n) priority queue operations vs O(n log n) for sorting an array repeatedly."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "array_functions",
        "generators",
        "time_complexity"
    ],
    "prerequisites": [
        "array_data_structure",
        "php_spl_data_structures",
        "big_o_notation"
    ],
    "refs": [
        "https://www.php.net/manual/en/spl.datastructures.php"
    ],
    "bad_code": "// Array misused as a queue — O(n) shift on large arrays:\n$queue = [];\narray_push($queue, $item);\n$next = array_shift($queue); // O(n) — reindexes entire array\n\n// SplQueue — O(1) enqueue and dequeue:\n$queue = new SplQueue();\n$queue->enqueue($item);\n$next = $queue->dequeue();",
    "good_code": "// SplStack — LIFO\n\\$stack = new SplStack();\n\\$stack->push('first'); \\$stack->push('second');\necho \\$stack->pop(); // 'second'\n\n// SplQueue — FIFO\n\\$queue = new SplQueue();\n\\$queue->enqueue('job1'); \\$queue->enqueue('job2');\necho \\$queue->dequeue(); // 'job1'\n\n// SplMinHeap — always extracts the minimum\n\\$heap = new SplMinHeap();\n\\$heap->insert(5); \\$heap->insert(1); \\$heap->insert(3);\necho \\$heap->extract(); // 1\n\n// SplFixedArray — faster, less memory than array for integer-indexed data\n\\$arr = new SplFixedArray(100);\n\\$arr[0] = 'hello';",
    "quick_fix": "Use SplStack, SplQueue, SplMinHeap for algorithm work where semantics matter — PHP arrays work for most cases but SPL structures have correct complexity guarantees and self-documenting intent",
    "severity": "low",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/spl_data_structures",
        "html_url": "https://codeclaritylab.com/glossary/spl_data_structures",
        "json_url": "https://codeclaritylab.com/glossary/spl_data_structures.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": "[SPL Data Structures](https://codeclaritylab.com/glossary/spl_data_structures) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/spl_data_structures"
            }
        }
    }
}