#native_company# #native_desc#

PHP Iterator Page 3

By Dejan Bosanac
on February 25, 2003


function getEmployees() {

$query "SELECT id FROM persons WHERE companyid = $this->companyid";

$stmt execute_query($query); // creates statement object

$this->employees = array();

while (
$row fetch_object($stmt) {

$this->employess[$row->id] = new Person($row->id); // object creation


return $this->employees;



and the usage would be:


= new Company("Datagate");

$employees $company->getEmployees();

foreach (
$employees as $id =>$employee)

$employee->addVacationDays(3); // object usage


OK, these look like fairly obvious solutions. But, it has few big flaws. All
objects are created but we don’t know if we’re going to use them all.
There are two performance problems here. First accessing a relational
database (for creating these objects) can be very time expensive. And if
the company has 500 employees and you need to access data for only 50, that is
lot of time wasted. Imagine now, that we are loading these objects through
RPC which is even slower. This could seriously affect application
performance. Now, even if all objects are needed we don’t need them at the
same time, we need objects one by one as we are traversing the array. The
solution above is a huge waste of resources (memory and time).