mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-15 16:38:54 +00:00
144 lines
2.9 KiB
XML
144 lines
2.9 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<sect1 xml:id="language.types.iterable">
|
|
<title>Iterables</title>
|
|
|
|
<para>
|
|
<type>Iterable</type> is a pseudo-type introduced in PHP 7.1. It accepts any
|
|
&array; or object implementing the <classname>Traversable</classname>
|
|
interface. Both of these types are iterable using &foreach; and can be used
|
|
with <command>yield from</command> within a <link
|
|
linkend="language.generators">generator</link>.
|
|
</para>
|
|
|
|
<sect2 xml:id="language.types.iterable.using">
|
|
<title>Using Iterables</title>
|
|
|
|
<para>
|
|
Iterable can be used as a parameter type to indicate that a function
|
|
requires a set of values, but does not care about the form of the value set
|
|
since it will be used with &foreach;. If a value is not an array or
|
|
instance of <classname>Traversable</classname>, a
|
|
<classname>TypeError</classname> will be thrown.
|
|
</para>
|
|
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Iterable parameter type example
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
function foo(iterable $iterable) {
|
|
foreach ($iterable as $value) {
|
|
// ...
|
|
}
|
|
}
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
|
|
<para>
|
|
Parameters declared as iterable may use &null; or an array as a default
|
|
value.
|
|
</para>
|
|
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Iterable parameter default value example
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
function foo(iterable $iterable = []) {
|
|
// ...
|
|
}
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
|
|
<para>
|
|
Iterable can also be used as a return type to indicate a function will
|
|
return an iterable value. If the returned value is not an array or instance
|
|
of <classname>Traversable</classname>, a <classname>TypeError</classname>
|
|
will be thrown.
|
|
</para>
|
|
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Iterable return type example
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
function bar(): iterable {
|
|
return [1, 2, 3];
|
|
}
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
|
|
<para>
|
|
Functions declaring iterable as a return type may also be <link
|
|
linkend="language.generators">generators</link>.
|
|
</para>
|
|
|
|
<para>
|
|
<example>
|
|
<title>
|
|
Iterable generator return type example
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
function gen(): iterable {
|
|
yield 1;
|
|
yield 2;
|
|
yield 3;
|
|
}
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
</sect2>
|
|
|
|
</sect1>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|