Book Image

Object-Oriented Programming with PHP5

By : Hasin Hayder
Book Image

Object-Oriented Programming with PHP5

By: Hasin Hayder

Overview of this book

<p>Some basic objected-oriented features were added to PHP3; with PHP5 full support for object-oriented programming was added to PHP. Object-oriented programming was basically introduced to ease the development process as well as reduce the time of development by reducing the amount of code needed. OOP can greatly improve the performance of a properly planned and designed program.</p> <p>This book covers all the general concepts of OOP then shows you how to make use of OOP in PHP5, with the aid of an ample number of examples.</p>
Table of Contents (15 chapters)
Object-Oriented Programming with PHP5
Credits
About the Author
About the Reviewers
Introduction
Index

ArrayIterator


ArrayIterator is used to iterate over the elements of an array. In SPL, ArrayObject has a built-in Iterator, which you can access using getIterator function. You can use this object to iterate over any collection. Let's take a look at the example here:

<?php
$fruits = array(  
    "apple" => "yummy",
    "orange" => "ah ya, nice",
    "grape" => "wow, I love it!",
    "plum" => "nah, not me"
);

$obj = new ArrayObject( $fruits );

$it = $obj->getIterator();

// How many items are we iterating over?
echo "Iterating over: " . $obj->count() . " values\n";

// Iterate over the values in the ArrayObject:
while( $it->valid() )
{
    echo $it->key() . "=" . $it->current() . "\n"; 
    $it->next();
}

?> 

This will output the following:

Iterating over: 4 values
apple=yummy
orange=ah ya, nice
grape=wow, I love it!
plum=nah, not me

However, an Iterator also implements the IteratorAggregator interface so you can even use them in the foreach() loop....