note 126948 added to function.iterator-to-array
| From: | wizzard351.yahoo.com via php-notes | Date: | Mon, 21 Mar 2022 15:55:58 +0000 |
| Subject: | note 126948 added to function.iterator-to-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217755@lists.php.net to get a copy of this message | ||
One important thing to remember is that in iterator can be infinite. Not all iterators necessarily
end. If iterator_to_array is used on such an iterator, it will exhaust the available memory, and
throw a fatal error.
For example, consider the following code:
<?php
function fibonacci(): Generator
{
yield $a = 1;
yield $b = 2;
start:
yield $c = $a + $b;
$a = $b;
$b = $c;
goto start;
}
$fibonacciSequence = fibonacci();
iterator_to_array($fibonacciSequence);
?>
Since <?php fibonacci(); ?> generates an infinite fibonacci sequence, which is valid, since it
is actually an infinite sequence, then attempting to convert it to an array will fail.
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 64.92.8.66)
----
Manual Page -- https://php.net/manual/en/function.iterator-to-array.php
Edit -- https://main.php.net/note/edit/126948
Del: integrated -- https://main.php.net/note/delete/126948/integrated
Del: useless -- https://main.php.net/note/delete/126948/useless
Del: bad code -- https://main.php.net/note/delete/126948/bad+code
Del: spam -- https://main.php.net/note/delete/126948/spam
Del: non-english -- https://main.php.net/note/delete/126948/non-english
Del: in docs -- https://main.php.net/note/delete/126948/in+docs
Del: other reasons-- https://main.php.net/note/delete/126948
Reject -- https://main.php.net/note/reject/126948
Search -- https://main.php.net/manage/user-notes.php