Since I'am not alway use the getter and setter to populate an object so here is my version of 'ObjectLoaderIterator', please give more comments
PHP Code:
class ObjectListIterator extends Iterator
{
var $aObject;
var $aIterator;
var $returnedObject;
/**
*
*
*
* * $object is the original object you want to get
* $queryIterator since we are talking about get object
* from database this is the reference to
* $populatorMethod This is the function name you
* used to populat your object.
* $populatorPara is the parameter your populatorMethod need.
*
*/
function ObjectListIterator(&$object, &$queryIterator, $populatorMethod=null, $populatorPara=null)
{
$this->$aObject=&$object;
$this->$aIterator = & $queryIterator;
}
function &next()
{
$this->aIterator->next();
$row = $this->aIterator->getCurrent(); // Only work for QueryIterator so we can assume it is a row;
$returnedObject = $this->aObject;//I can't find the clone() method in manual.
if(is_array($populatorPara))
{
$stringBegin='$this->returnedObject->$populator(';
$stringClose = ');';
foreach($populatorPara as $key=>$val)
{
$paraString .='$row[\'$val\'],';
}
$paraString = substr($paraString,0,-1); //remove the last comma
$executeString =$stringBegin.$paraString.$stringClose;
eval($executeString);
}
else
{
$this->returnedObject->$populator($row[$populatorPara]);
}
}
function reset()
{
$this->aIterator->reset();
}
function isValid()
{
//ofcourse we need some rule to check if it is valid.
}
function & getCurrent()
{
return $this->returnedObject;
}
}
You need bundle all your setters and getters into one function and use it in the iterator.
What is the solution in Java or other OOP language?
Ji Tao
Bookmarks