Introduction to Iterators Marcus Brger international PHP 2004

Introduction to Iterators Marcus Börger international PHP 2004 conference – spring edition Marcus Börger Introduction to Iterators

Introduction to Iterators þ þ What are Iterators The basic concepts Marcus Börger Introduction to Iterators 2

What are Iterators þ Iterators are a concept to iterate anything that contains other things. Examples: þ þ þ þ Values and Keys in an array Textlines in a file Database query results Files in a directory Elements or Attributes in XML Bits in an image Dates in a calendar range Iterators allow to encasulate algorythms Marcus Börger Introduction to Iterators 3

The basic concepts þ Iterators can be internal or external also referred to as active or passive þ An internal iterator modifies the object itself þ An external iterator points to another object without modifying it þ PHP always uses external iterators at engine-level Marcus Börger Introduction to Iterators 4

PHP Iterators þ þ Anything that can be iterated implements Traversable User classes cannot implement Traversable Aggregateis used for objects that use external iterators Iteratoris used for internal traversal or external iterators Marcus Börger Introduction to Iterators 5

Implementing Iterators Marcus Börger Introduction to Iterators 6

How Iterators work þ þ Iterators can be used manually Iterators can be used implicitly with foreach <? php $o = new Array. Iterator(array(1, 2, 3)); $o->rewind(); while ($o->valid) { $key = $o->key(); $val = $o->current(); // some code $o->next(); } ? > <? php $o = new Array. Iterator(array(1, 2, 3)); foreach($o as $key => $val) { // some code } ? > Marcus Börger Introduction to Iterators 7

Debug Session <? php class Array. Iterator { protected $ar; function __construct(Array $ar) { $this->ar = $ar; } function rewind() { rewind($this->ar); } fucntion valid() { return !is_null(key($this->ar)); } function key() { return key($this->ar); } fucntion current() { return current($this->ar); } function next() { next($this->ar); } } ? > Marcus Börger <? php $a = array(1, 2, 3); $o = new Array. Iterator($a); foreach($o as $key => $val) { echo "$key => $van"; } ? > 0 => 1 1 => 2 2 => 3 Introduction to Iterators 8

Array and property traversal þ Array. Objectallows external traversal of arrays and object properties þ Array. Objectcreates Array. Iteraorinstances for iteration þ Multiple Array. Iteratorinstances can reference the same target with different states Marcus Börger Introduction to Iterators 9

Array and property traversal Marcus Börger Introduction to Iterators 10

Recursive traversal þ þ þ Some data may be recursively traversable Interface Recursive. Iterator tells when Class Recursive. Iterator use it þ Examples: þ Arrays þ XML data þ Directories Marcus Börger Introduction to Iterators 11

Recursive traversal Marcus Börger Introduction to Iterators 12

Filtering values þ Filter. Iteratorallows to filter data Compareable to SQL WHERE clauses þ Filter. Iterator: : __construct takes any Iterator þ Filter. Iterator : : accept needs to be implemented þ Specializations: þ Search. Iteratorstops at the first accepted value þ Parent. Iteratoronly accepts values which have childs Marcus Börger Introduction to Iterators 13

Filtering values Marcus Börger Introduction to Iterators 14

Limiting values þ Limit. Iteratorallows to limit the returned values Compareable to LIMITof some SQL dialects þ You can specify the start offset þ You can specify the number of returned values þ When the inner Iterator is a Seekable. Iteratorthen method seek will be used. Otherwise seek operation will be manually. Marcus Börger Introduction to Iterators 15

Limiting values Marcus Börger Introduction to Iterators 16

Appending Iterators þ Append. Iteratorallows to concatenate Iterators Compareable to SQL clause UNION þ Uses a private Array. Iteratorto store Iterators þ Append. Iterator : : append() þ allows to append iterators þ does not call the rewind() þ if $this is invalid $this will move to the appended iterator Marcus Börger Introduction to Iterators 17

Appending Iterators Marcus Börger Introduction to Iterators 18

Getting rid of rewind þ No. Rewind. Iterator allows to omit rewind calls This is especially helpful when appending with þ Array. Object: : append () þ Array. Iterator: : append () þ Appenditerator: : append () Marcus Börger Introduction to Iterators 19

Getting rid of rewind() Marcus Börger Introduction to Iterators 20

Vacuity & Infinity Sometimes it is helpful to have þ Empty. Iteratoras a placeholder for no data þ Infinite. Iterator to endlessly repeat data in an iterator Marcus Börger Introduction to Iterators 21

Vacuity & Infinity Marcus Börger Introduction to Iterators 22

has. Next ? þ Caching. Iteratorcaches the current element þ This allows to know whether one more value exists þ Caching. Recursive. Iterator does this recursively þ This allows to draw tree graphics marcus@frodo /usr/src/php-cvs $ php ext/spl/examples/tree. php ext/spl |-CVS |-examples | |-CVS | -tests | -CVS -tests -CVS Marcus Börger Introduction to Iterators 23

has. Next ? Marcus Börger Introduction to Iterators 24

References þ Documentation and Sources to PHP 5 http: //php. net þ Documentation to ext/spl http: //cvs. php. net/co. php/php-src/ext/spl. php? r=HEAD http: //somabo. de/php/ext/spl/html/ þ Sourcecode for examples ext/spl/examples þ These slides http: //somabo. de/talks/ Marcus Börger Introduction to Iterators 25
- Slides: 25