Class: Alt
- Package: saf.Misc
- Author: John Luxford <lux@simian.ca>
- Copyright: Copyright (C) 2001-2003, Simian Systems Inc.
- License: http://www.sitellite.org/index/license Simian Open Software License
- Version: 1.2, 2002-11-13, $Id: Alt.php,v 1.3 2007/10/06 00:06:30 lux Exp $
- Access: public
Alt is a tiny class that alternates through a list of values
each time you call the next () method. When it gets to the end of the
list, the pointer is reset to the beginning.
This class is a little redundant, as a simple array would usually
suffice, but the Object Oriented syntax is nicer, and our original
inspiration for this class was a two element array of background
colours for table rows. The equivalent using an array still required
an if statement, which this eliminates.
New in 1.2:
- Added a reset() method.
Usage Example
<?php
$c = new Alt ('odd', 'even');
while (...) {
echo $c->next ();
}
--
The equivalent using a single variable:
$c = 'odd';
while (...) {
echo $c;
if ($c == 'odd') {
$c = 'even';
} else {
$c = 'odd';
}
}
--
The equivalent using an array:
$c = array ('odd', 'even');
$i = 0;
while (...) {
echo $c[$i];
if ($i >= count ($c)) {
$i = 0;
}
}
Using arrays, the next(), current(), each(), end(), etc. functions are
not appropriate when the alternating values are not the focus of the
loop, but are still required to change.
?>
Return to Top
Properties
$vals
List of values to alternate between.
$counter
The internal position counter.
$current
Always keeps the value of the current array position here.
Return to Top
Methods
Alt ($vals, $val2 = '', $val3 = '')
Constructor method. If the first parameter is an array,
it is used to set the $vals property. If not, up to three parameters
can be passed, which are fed into the $vals property array.
next ()
- Access: public
- Return: mixed
Advances the internal counter and returns the next value.
reset ()
Returns the internal counter to 0 and returns nothing.
Return to Top