#native_company# #native_desc#
#native_cta#

PHP Iterator Page 4

By Dejan Bosanac
on February 25, 2003

The solution to these performance problems looks so obvious. Let’s return
just an array of employee ids. The code would look something like this:

<?php

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] = $row->id;

}

return $this->employees;

}

?>



and the usage would be:

<?php

$company 
= new Company("Datagate");

$employees $company->getEmployees();

foreach (
$employees as $id) {

    
$employee = new Employee($id); // object creation

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

}

?>



This looks fine at the first sight. We have saved time and memory
, but another problem has arisen. Suppose that the code for creating
Employee object changes, for example you need to add extra argument to the
constructor or some extra initialization (these are things that are
happening on real projects). In that case you’ll need to modify your code in
many places (wherever you have used getEmployees() method), And that is
a problem.