PHP Design Patterns Talk: Iterator Pattern (5)_PHP Tutorial
注意,迭代器是比对象集更好的抽象,因为我们可以让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;
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原文名: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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
