#native_company# #native_desc#
#native_cta#

PHP Iterator Page 2

By Dejan Bosanac
on February 25, 2003

OK, you say, this is all nice but there is nothing I can’t do with
combination of native PHP functions. Besides the fact that you are accessing all your
arrays through unique interface, another (and most important) advantage
is that Iterator’s object structure allows you to easily expand its
functionality.

ObjectIterator interface

Often I have ended up in situations where my object methods had to return
an array of some other object as a result. Usually that object is loaded
from the database, but could be some other situation such as obtaining
objects through some RPC protocol (XML-RPC, SOAP, …) or endless
other situation combinations that you experience every day. In this article we
will focus on the first problem and briefly explain how to empower
Iterator for the purpose you’d need.
Suppose that you are developing an address book for some large web
application. Your address book will work with companies and persons. In
addition, companies could have an endless number of employees (that are also
kinds of persons). So far we have recognized two objects in our application:
Company and Person. Also, it is clear that the company will have method
getEmployees() that returns an array of Person objects. There are a number of
possible implementations of this method. Here are some usual
implementations:
First, you could write a query to collect all the ids of all the company
employees. Then you could make an array that contains all the objects and
returns this array. This would look something like this (supposing you have a
database wrapper):