2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:17:58 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml:id="language.oop5.iterations" xmlns="http://docbook.org/ns/docbook">
|
2004-07-10 19:30:37 +00:00
|
|
|
<title>Object Iteration</title>
|
|
|
|
<para>
|
2004-10-28 02:50:22 +00:00
|
|
|
|
2020-12-15 01:40:52 +00:00
|
|
|
PHP provides a way for objects to be defined so it is possible to iterate
|
2009-02-16 15:30:18 +00:00
|
|
|
through a list of items, with, for example a &foreach; statement. By default,
|
2004-10-28 02:54:15 +00:00
|
|
|
all <link linkend="language.oop5.visibility">visible</link> properties will be used
|
2004-10-28 02:50:22 +00:00
|
|
|
for the iteration.
|
|
|
|
|
2004-07-10 19:30:37 +00:00
|
|
|
</para>
|
|
|
|
|
2004-07-23 20:06:04 +00:00
|
|
|
<example>
|
|
|
|
<title>Simple Object Iteration</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2004-10-02 09:40:52 +00:00
|
|
|
class MyClass
|
|
|
|
{
|
|
|
|
public $var1 = 'value 1';
|
|
|
|
public $var2 = 'value 2';
|
|
|
|
public $var3 = 'value 3';
|
2004-07-23 20:06:04 +00:00
|
|
|
|
2004-10-28 02:50:22 +00:00
|
|
|
protected $protected = 'protected var';
|
|
|
|
private $private = 'private var';
|
|
|
|
|
|
|
|
function iterateVisible() {
|
|
|
|
echo "MyClass::iterateVisible:\n";
|
2016-01-15 17:34:57 +00:00
|
|
|
foreach ($this as $key => $value) {
|
2004-10-28 02:50:22 +00:00
|
|
|
print "$key => $value\n";
|
|
|
|
}
|
|
|
|
}
|
2004-07-23 20:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$class = new MyClass();
|
|
|
|
|
|
|
|
foreach($class as $key => $value) {
|
2004-10-02 09:40:52 +00:00
|
|
|
print "$key => $value\n";
|
2004-07-23 20:06:04 +00:00
|
|
|
}
|
2004-11-08 12:10:21 +00:00
|
|
|
echo "\n";
|
2004-10-28 02:50:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
$class->iterateVisible();
|
|
|
|
|
2004-10-17 11:09:29 +00:00
|
|
|
?>
|
2004-07-23 20:06:04 +00:00
|
|
|
]]>
|
|
|
|
</programlisting>
|
2004-10-02 09:40:52 +00:00
|
|
|
&example.outputs;
|
2004-07-23 20:06:04 +00:00
|
|
|
<screen role="php">
|
|
|
|
<![CDATA[
|
|
|
|
var1 => value 1
|
|
|
|
var2 => value 2
|
|
|
|
var3 => value 3
|
2004-10-28 02:50:22 +00:00
|
|
|
|
|
|
|
MyClass::iterateVisible:
|
|
|
|
var1 => value 1
|
|
|
|
var2 => value 2
|
|
|
|
var3 => value 3
|
|
|
|
protected => protected var
|
|
|
|
private => private var
|
2004-07-23 20:06:04 +00:00
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
|
|
|
|
</example>
|
|
|
|
|
|
|
|
<para>
|
2012-03-27 22:42:19 +00:00
|
|
|
As the output shows, the &foreach; iterated through all of the
|
|
|
|
<link linkend="language.oop5.visibility">visible</link> properties that could be
|
|
|
|
accessed.
|
|
|
|
</para>
|
2004-07-23 20:06:04 +00:00
|
|
|
|
2021-04-20 09:28:39 +00:00
|
|
|
<simplesect role="seealso">
|
|
|
|
&reftitle.seealso;
|
2004-07-23 20:06:04 +00:00
|
|
|
<para>
|
2021-04-20 09:28:39 +00:00
|
|
|
<simplelist>
|
|
|
|
<member><link linkend="language.generators">Generators</link></member>
|
|
|
|
<member><interfacename>Iterator</interfacename></member>
|
|
|
|
<member><interfacename>IteratorAggregate</interfacename> </member>
|
|
|
|
<member><link linkend="spl.iterators">SPL Iterators</link></member>
|
|
|
|
</simplelist>
|
2004-07-23 20:06:04 +00:00
|
|
|
</para>
|
2021-04-20 09:28:39 +00:00
|
|
|
</simplesect>
|
2012-11-20 15:17:23 +00:00
|
|
|
|
2004-07-10 19:30:37 +00:00
|
|
|
</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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2004-07-10 19:30:37 +00:00
|
|
|
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
|
|
|
|
-->
|