note 108616 added to function.next

From: Date: Thu, 10 May 2012 22:51:39 +0000
Subject: note 108616 added to function.next
Groups: php.notes 
Request: Send a blank email to php-notes+get-187925@lists.php.net to get a copy of this message
I use this class class Cycle { private $items = array(); private $count = 0; private $index = 0; function __construct() { $this->items = func_get_args(); $this->count = $this->countItems(); } public function getCurrent() { if ( $this->count <= 0 ) { throw new Exception('There are no items in your list.'); } return $this->items[$this->index++ % $this->count]; } public function resetPosition() { $this->index = 0; } public function addItem($i) { $this->items[] = (string)$i; $this->countItems(); } public function getItems() { return var_dump($this->items); } public function resetItems() { $this->items = array(); $this->resetPosition(); } private function countItems() { $this->count = count($this->items); return count($this->items); } } $myCycle = new Cycle(); $myCycle->addItem('dog'); $myCycle->addItem('house'); echo $myCycle->getCurrent() . "\n"; echo $myCycle->getCurrent() . "\n"; echo $myCycle->getCurrent() . "\n"; echo "\n"; $myCycle = new Cycle('meow','box'); $myCycle->addItem('cat'); echo $myCycle->getCurrent() . "\n"; echo $myCycle->getCurrent() . "\n"; echo $myCycle->getCurrent() . "\n"; echo $myCycle->getCurrent() . "\n"; ---- Server IP: 69.147.83.197 Probable Submitter: 72.5.59.140 ---- Manual Page -- http://www.php.net/manual/en/function.next.php Edit -- https://master.php.net/note/edit/108616 Del: integrated -- https://master.php.net/note/delete/108616/integrated Del: useless -- https://master.php.net/note/delete/108616/useless Del: bad code -- https://master.php.net/note/delete/108616/bad+code Del: spam -- https://master.php.net/note/delete/108616/spam Del: non-english -- https://master.php.net/note/delete/108616/non-english Del: in docs -- https://master.php.net/note/delete/108616/in+docs Del: other reasons-- https://master.php.net/note/delete/108616 Reject -- https://master.php.net/note/reject/108616 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#187925) next »