25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Elements of Object-oriented Design ” 183<br />

} else {<br />

return ($this->current() % 2 == 1);<br />

}<br />

}<br />

}<br />

$numbers = new ArrayObject(range(0, 10));<br />

$numbers_it = new ArrayIterator($numbers);<br />

$it = new NumberFilter($numbers_it, NumberFilter::FILTER_ODD);<br />

foreach ($it as $number) {<br />

echo $number . <strong>PHP</strong>_EOL;<br />

}<br />

The accept() method simply determines whether any given element should be allowed<br />

in the iteration; note that FilterIterator already implements all of the methods<br />

of ArrayAccess, so that, effectively, from the outside our class can still be used as<br />

an array.<br />

This example outputs only the odd numbers stored in the array:<br />

1<br />

3<br />

5<br />

7<br />

9<br />

Summary<br />

Object Oriented programming, coupled with Design Patterns—including those provided<br />

by SPL—is the key to re-usable and highly modular code.<br />

The more forethought you give your design, the more likely you are to be able to<br />

re-use at least some of it, later, saving time and effort in the future—not to mention<br />

that proper design techniques also make code easier to maintain and extend.<br />

Bringing this experience to the table is what makes you truly versatile; as we mentioned,<br />

design patterns are, after all, largely language- and problem-independent.<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!