note 91921 added to language.oop5.iterations
| From: | Peter' | Date: | Wed, 01 Jul 2009 17:12:18 +0000 |
| Subject: | note 91921 added to language.oop5.iterations | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157784@lists.php.net to get a copy of this message | ||
This is what seems to work for checking isFirst() and isLast() on a class implementing the Iterator
interface - without destroying the internal array pointer:
// code:
// Let $this->_elements be your internal array with
// array("one", "two", "three")
public function isFirst()
{
$hasPrevious = prev($this->_elements);
// now undo
if ($hasPrevious) {
next($this->_elements);
} else {
reset($this->_elements);
}
return !$hasPrevious;
}
public function isLast()
{
$hasNext = next($this->_elements);
// now undo
if ($hasNext) {
prev($this->_elements);
} else {
end($this->_elements);
}
return !$hasNext;
}
// usage
foreach ($myInterator as $value) {
echo $value;
if ($myInterator->isFirst()) {
echo " (first)";
}
if ($myInterator->isLast()) {
echo " (last)";
}
echo " - value is still the same: ", $value, "<br />";
}
// output
one (first) - value is still the same: one
two - value is still the same: two
three (last) - value is still the same: three
This can be helpfull for designing CSS elements where the first or last element must have a
different padding/margin than the others.
----
Server IP: 212.124.37.9
Probable Submitter: 89.244.130.51
----
Manual Page -- http://www.php.net/manual/en/language.oop5.iterations.php
Edit -- https://master.php.net/note/edit/91921
Del: integrated -- https://master.php.net/note/delete/91921/integrated
Del: useless -- https://master.php.net/note/delete/91921/useless
Del: bad code -- https://master.php.net/note/delete/91921/bad+code
Del: spam -- https://master.php.net/note/delete/91921/spam
Del: non-english -- https://master.php.net/note/delete/91921/non-english
Del: in docs -- https://master.php.net/note/delete/91921/in+docs
Del: other reasons-- https://master.php.net/note/delete/91921
Reject -- https://master.php.net/note/reject/91921
Search -- https://master.php.net/manage/user-notes.php