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 arraysthe next(), current(), each(), end(), etcfunctions 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

  • Access: public

List of values to alternate between.


$counter

  • Access: private

The internal position counter.


$current

  • Access: private

Always keeps the value of the current array position here.

Return to Top



Methods


Alt ($vals, $val2 = '', $val3 = '')

  • Access: public

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 ()

  • Access: public

Returns the internal counter to 0 and returns nothing.

Return to Top

Copyright © 2008 Sitellite CMS Project

Powered by Sitellite 5.0 Content Management System