PHPBuilder - PHP Iterator Page 3



RSS Twitter
Articles Application Architecture

PHP Iterator - Page 3

by: Dejan Bosanac
|
February 25, 2003


<?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] = new Person($row->id); // object creation
}

return 
$this->employees;
}
?>
and the usage would be:

<?php
$company 
= 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).

« 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.