php教程|php手册
PHP,设计模式,漫谈,之,迭代,器,模式,注意,迭代,器,是,比,对象,集更,好抽象,因为,我们,可以,让,Inf
php教程-php手册
注意,迭代器是比对象集更好的抽象,因为我们可以让InfiniteIterators,NoRewindIterators等,不用与普通数组阵列与一致,因此,Iterator缺少count()函数等功能。
在PHP官方手册中可以找到完整的SPL迭代器列表。得益于对PHP的强力支持,使用迭代器模式的大部分工作都包括在标准实现中,下面的代码示例就利用了标准Iterator和RecursiveIterators的功能。
___FCKpd___0___FCKpd___1
// lets play with RecursiveIterator implementations $it = new RecursiveArrayIterator(array(A,B,array(C,D),array(array( E, F),array( G, H, I)) )); // $it is a RecursiveIterator but also an Iterator, // so it loops normally over the four elements // of the array. echo “Foreach over a RecursiveIterator: “; foreach ($it as $value) {echo $value;// but RecursiveIterators specify additional// methods to explore children nodes$children = $it->hasChildren() ? {Yes} : {No};echo $children, ; } echo “
“; // we can bridge it to a different contract via // a RecursiveIteratorIterator, whose cryptic name // should be read as an Iterator that spans over // a RecursiveIterator. echo “Foreach over a RecursiveIteratorIterator: “; foreach (new RecursiveIteratorIterator($it) as $value) {echo $value; } echo “
“; 原文名:Practical Php Patterns: Iterator 作者:Giorgio
content;
private $_index = 0;
下载的网站源码怎么上传,ubuntu键盘移动鼠标,浴室黑色小爬虫,腾讯php jsp,seo排名公式lzw
public function __construct(array $content)
{
$this->_content = $content;
}
自行车在线租赁源码,ubuntu 脚本配置网络,怎么饲养观赏爬虫,php 如何截取,唐山seo管理lzw
public function rewind()
{
$this->_index = 0;
}
抓取别人微信网页源码,ubuntu拨号断网,为什么要安装tomcat,爬虫技术结果,php 头部信息,襄阳工程机械seo推广哪家好lzw
public function valid()
{
return isset($this->_content[$this->_index]);
}
public function current()
{
return $this->_content[$this->_index];
}
public function key()
{
return $this->_index;
}
public function next()
{
$this->_index++;
}
}
$arrayarray = array(A, B, C, D);
echo “Collection: “;
foreach (new Collection($array) as $key => $value) {
echo “$key => $value. “;
}
echo “
“;
___FCKpd___1___FCKpd___2原文名:Practical Php Patterns: Iterator 作者:Giorgio
content;
public function __construct(array $content)
{
$this->_content = $content;
}
public function contains($number)
{
return in_array($number, $this->_content);
}
/**
* Only this method is necessary to implement IteratorAggregate.
* @return Iterator
*/
public function getIterator()
{
return new ArrayIterator($this->_content);
}
}
echo “NumbersSet: “;
foreach (new NumbersSet($array) as $key => $value) {
echo “$key => $value. “;
}
echo “
“;
___FCKpd___2原文名:Practical Php Patterns: Iterator 作者:Giorgio
content;
private $_index = 0;
public function __construct(array $content)
{
$this->_content = $content;
}
public function rewind()
{
$this->_index = 0;
}
public function valid()
{
return isset($this->_content[$this->_index]);
}
public function current()
{
return $this->_content[$this->_index];
}
public function key()
{
return $this->_index;
}
public function next()
{
$this->_index++;
}
}
$arrayarray = array(A, B, C, D);
echo “Collection: “;
foreach (new Collection($array) as $key => $value) {
echo “$key => $value. “;
}
echo “
“;
___FCKpd___1___FCKpd___2