PHPBuilder - PHP Iterator Page 4



RSS Twitter
Articles Application Architecture

PHP Iterator - Page 4

by: Dejan Bosanac
|
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.

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Dejan Bosanac

Comment:



Comment:

(Maximum characters: 1200). You have characters left.