Aidan's PHP Repository

A repository for PHP functions and classes ...

Cycle.php

A simple cycle class for alternating between strings.

  • Author: Aidan Lister <aidan@php.net>
  • Version: 1.0.1
  • Link: http://aidanlister.com/repos/v/Cycle.php
  • Views: 7500
  • Downloads: 1549

Source

Download this script <?php
/**
 * A simple cycle class for alternating between strings.
 *
 * @author      Aidan Lister <aidan@php.net>
 * @version     1.0.1
 * @link        http://aidanlister.com/repos/v/Cycle.php
 */
class Cycle
{
    /**
     * Array of strings to be cycled
     *
     * @var     array
     */
    private $_args;
 
 
    /**
     * The current position in the stack
     *
     * @var     array
     */
    private $_key;
    
 
    /**
     * Constructor
     *
     * @param   overloader  Strings to be cycled through
     */
    function __construct()
    {
        $this->_args = func_get_args();
        $this->_key = -1;
    }
    
 
    /**
     * Convert to a string
     *
     * @return  string      The next string in the stack
     */
    function __toString()
    {
        return (string) isset($this->_args[$this->_key += 1]) ?
            $this->_args[$this->_key] :
            $this->_args[$this->_key = 0] ;
    }
}
 
?>

Comments

June 28th, 2006
although of more limited use, i accomplished similar functionality using just a function.
January 9th, 2006
very nice script.. i always made this with a function and a global var :) but this way is more comfortable
October 31st, 2005
Wow, at first I was wondering what in the world this could be used for. But now that I thought about it, it can be very useful. Thanks aidan.