Fix #74296: add documentation for iterable pseudo-type and is_iterable()

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@342265 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Jiří Pudil 2017-04-08 17:03:13 +00:00
parent 62d1457626
commit 86d8c6800b
4 changed files with 289 additions and 1 deletions

View file

@ -43,7 +43,7 @@
</itemizedlist>
<para>
Three compound types:
Four compound types:
</para>
<itemizedlist>
@ -66,6 +66,12 @@
</simpara>
</listitem>
<listitem>
<simpara>
<type>iterable</type>
</simpara>
</listitem>
</itemizedlist>
<para>
@ -207,6 +213,7 @@ if (is_string($a_bool)) {
&language.types.float;
&language.types.string;
&language.types.array;
&language.types.iterable;
&language.types.object;
&language.types.resource;
&language.types.null;

180
language/types/iterable.xml Normal file
View file

@ -0,0 +1,180 @@
<?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>
<sect2 xml:id="language.types.iterable.variance">
<title>Iterable Type Variance</title>
<para>
Classes extending/implementing may broaden methods using &array; or
<classname>Traversable</classname> as parameter types to
<type>iterable</type> or narrow return types from <type>iterable</type> to
&array; or <classname>Traversable</classname>.
</para>
<para>
<example>
<title>
Iterable type variance example
</title>
<programlisting role="php">
<![CDATA[
<?php
interface Example {
public function method(array $array): iterable;
}
class ExampleImplementation implements Example {
public function method(iterable $iterable): array {
// Parameter broadened and return type narrowed.
}
}
?>
]]>
</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
-->

View file

@ -0,0 +1,100 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.is-iterable" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>is_iterable</refname>
<refpurpose>
Verify that the contents of a variable is an iterable value
</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>bool</type><methodname>is_iterable</methodname>
<methodparam><type>mixed</type><parameter>var</parameter></methodparam>
</methodsynopsis>
<para>
Verify that the contents of a variable is accepted by the
<type>iterable</type> pseudo-type, i.e. that it is an <type>array</type> or
an object implementing <classname>Traversable</classname>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>var</parameter></term>
<listitem>
<para>
The value to check
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns &true; if <parameter>var</parameter> is iterable, &false;
otherwise.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>is_iterable</function> examples</title>
<programlisting role="php">
<![CDATA[
<?php
var_dump(is_iterable([1, 2, 3])); // bool(true)
var_dump(is_iterable(new ArrayIterator([1, 2, 3]))); // bool(true)
var_dump(is_iterable((function () { yield 1; })())); // bool(true)
var_dump(is_iterable(1)); // bool(false)
var_dump(is_iterable(new stdClass())); // bool(false)
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>is_array</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
-->

View file

@ -22,6 +22,7 @@
<function name='is_float' from='PHP 4, PHP 5, PHP 7'/>
<function name='is_int' from='PHP 4, PHP 5, PHP 7'/>
<function name='is_integer' from='PHP 4, PHP 5, PHP 7'/>
<function name="is_iterable" from="PHP 7 &gt;= 7.1.0" />
<function name='is_long' from='PHP 4, PHP 5, PHP 7'/>
<function name='is_null' from='PHP 4 &gt;= 4.0.4, PHP 5, PHP 7'/>
<function name='is_numeric' from='PHP 4, PHP 5, PHP 7'/>