--- id: php-iterables title: "PHP Iterables" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Iterator interface", "PHP μ΄ν„°λŸ¬λΈ”"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["php", "programming", "w3schools", "iterables", "iterator"] raw_sources: ["https://www.w3schools.com/php/php_iterables.asp"] applied_in: [] github_commit: "" --- # [[PHP Iterables]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The `iterable` type hint accepts EITHER a plain array OR any object implementing the `Iterator` interface β€” meaning a function declared `function printIterable(iterable $x)` works identically whether called with `["a","b","c"]` or a custom `MyIterator` object, since both satisfy the same "can be foreach'd" contract. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`iterable`** (PHP 7.1+) β€” a pseudo-type usable as a function parameter or return type; means "anything loopable with foreach." [S1] - **Two things satisfy `iterable`** β€” any array, or any object implementing the `Iterator` interface. [S1] - **`Iterator` interface β€” 5 required methods** β€” `current()` (return current element), `key()` (return current key β€” int/float/bool/string only), `next()` (advance pointer), `rewind()` (reset pointer to start), `valid()` (false if pointer is past the end). [S1] - **Custom iterator pattern** β€” implement all 5 methods manually to make any custom class loopable via foreach. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Iterable parameter accepting both array and iterator object: `function printIterable(iterable $x) { foreach($x as $item) { echo $item; } } printIterable(["a", "b", "c"]); $iterator = new ArrayIterator(["d", "e", "f"]); printIterable($iterator);`. [S1] - Iterable return type: `function getIterable():iterable { return ["a", "b", "c"]; } foreach(getIterable() as $item) { echo $item; }`. [S1] - Custom Iterator implementation: `class MyIterator implements Iterator { private $items = []; private $pointer = 0; public function __construct($items) { $this->items = array_values($items); } public function current() { return $this->items[$this->pointer]; } public function key() { return $this->pointer; } public function next() { $this->pointer++; } public function rewind() { $this->pointer = 0; } public function valid() { return $this->pointer < count($this->items); } }`. [S1] - Using the custom iterator as an iterable: `$iterator = new MyIterator(["a", "b", "c"]); printIterable($iterator);`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) μ†ŒμŠ€μ—μ„œ λͺ¨μˆœλ˜λŠ” μ •λ³΄λŠ” λ°œκ²¬λ˜μ§€ μ•ŠμŒ. ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” λ°°μ—΄κ³Ό μ»€μŠ€ν…€ μ΄ν„°λ ˆμ΄ν„° 객체λ₯Ό λ™μΌν•œ ν•¨μˆ˜μ— 전달할 수 μžˆλŠ” 것이 iterable νƒ€μž… 힌트의 μ‹€μ „ κ°€μΉ˜λ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) A function accepting both an array and an Iterator object via iterable (PHP): ```php function printIterable(iterable $x) { foreach ($x as $item) { echo $item; } } printIterable(["a", "b", "c"]); // array works printIterable(new ArrayIterator(["d", "e", "f"])); // Iterator object works too ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.89 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP Namespaces]], [[PHP MySQL Intro]], [[PHP Looping Foreach]] - **μ°Έμ‘° λ§₯락:** μ–Έμ–΄ 핡심 λ¬Έλ²•μ˜ λ§ˆμ§€λ§‰ 챕터 β€” MySQL 연동 μ„Ήμ…˜μœΌλ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP Iterables β€” https://www.w3schools.com/php/php_iterables.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP Iterables" page (Astra wiki-curation, P-Reinforce v3.1 format).