Is there a cleaner way to loop over references?
Posted: Sat Aug 27, 2005 7:16 pm
I've ended up with a slightly crufty function:
I'd rather not introduce the two extra local variables $i and $numHandlers. However I couldn't a foreach or a while ( $foo=&next()) style loop to work. (For instance:
does not preserve the references. (PHP4 so I can't place the & before $value in a foreach).
Is there anyway to make this work, or should I just stick with the extra local variables?
Code: Select all
function _invokePreHandlers(&$module,&$con,&$req) {
$preHandlers =& $module->createPreHandlers();
$numHandlers=count($preHandlers);
$view="";
for ($i=0;$i<$numHandlers;$i++) {
$aHandler=&$preHandlers[$i];
if ($aHandler->canHandle($con,$req)) {
$handled=TRUE;
$view=$aHandler->execute($con,$req);
break;
}
}
return $view;
}Code: Select all
function _invokePreHandlers1(&$module,&$con,&$req) {
$preHandlers =& $module->createPreHandlers();
$view="";
reset($preHandlers);
while ($view=="" && $aHandler=&next($preHandlers))
if ($aHandler->canHandle($con,$req))
$view=$aHandler->execute($con,$req);
return $view;
}Is there anyway to make this work, or should I just stick with the extra local variables?