note 81431 added to function.current

From: Date: Thu, 28 Feb 2008 05:07:34 +0000
Subject: note 81431 added to function.current
Groups: php.notes 
Request: Send a blank email to php-notes+get-136543@lists.php.net to get a copy of this message
It took me a while to figure this out, but there is a more consistent way to figure out whether you really went past the end of the array, than using each(). You see, each() gets the value BEFORE advancing the pointer, and next() gets the value AFTER advancing the pointer. When you are implementing the Iterator interface, therefore, it's a real pain in the behind to use each(). And thus, I give you the solution: To see if you've blown past the end of the array, use key($array) and see if it returns NULL. If it does, you're past the end of the array -- keys can't be null in arrays. Nifty, huh? Here's how I implemented the Iterator interface in one of my classes: <?php /** * DbRow file * @package PalDb */ /** * This class lets you use Db rows and object-relational mapping functionality. */ class DbRow implements Iterator { /** * The DbResult object that gave us this row through fetchDbRows * @var DbResult */ protected $result; /** * The fields of the row * @var $fields */ protected $fields; /** * Constructor * * @param PDOStatement $stmt * The PDO statement object that this result uses * @param DbResult $result * The result that produced this row through fetchDbRows */ function __construct($result) { $this->result = $result; } /** * Get the DbResult object that gave us this row through fetchDbRows * @return DbResult * * @return unknown */ function getResult() { return $this->result; } function __set( $name, $value) { $this->fields[$name] = $value; } function __get( $name) { if (isset($this->fields[$name])) return $this->fields[$name]; else return null; } /** * Iterator implementation - rewind */ function rewind() { $this->beyondLastField = false; return reset($this->fields); } function valid() { return !$this->beyondLastField; } function current() { return current($this->fields); } function key() { return key($this->fields); } function next() { $next = next($this->fields); $key = key($this->fields); if (isset($key)) { return $next[1]; } else { $this->beyondLastField = true; return false; // doesn't matter what we return here, see valid() } } private $beyondLastField = false; }; Hope this helps someone. ---- Server IP: 64.71.164.2 Probable Submitter: 24.185.179.103 ---- Manual Page -- http://www.php.net/manual/en/function.current.php Edit -- https://master.php.net/note/edit/81431 Del: integrated -- https://master.php.net/note/delete/81431/integrated Del: useless -- https://master.php.net/note/delete/81431/useless Del: bad code -- https://master.php.net/note/delete/81431/bad+code Del: spam -- https://master.php.net/note/delete/81431/spam Del: non-english -- https://master.php.net/note/delete/81431/non-english Del: in docs -- https://master.php.net/note/delete/81431/in+docs Del: other reasons-- https://master.php.net/note/delete/81431 Reject -- https://master.php.net/note/reject/81431 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136543) next »