SPL提供了6個(gè)迭代器接口:
| Traversable | 遍歷接口(檢測(cè)一個(gè)類是否可以使用?foreach?進(jìn)行遍歷的接口) |
| Iterator | 迭代器接口(可在內(nèi)部迭代自己的外部迭代器或類的接口) |
| IteratorAggregate | 聚合式迭代器接口(創(chuàng)建外部迭代器的接口) |
| OuterIterator | 迭代器嵌套接口(將一個(gè)或多個(gè)迭代器包裹在另一個(gè)迭代器中) |
| RecursiveIterator | 遞歸迭代訪問(wèn)接口(提供遞歸訪問(wèn)功能) |
| SeekableIterator | 可索引迭代訪問(wèn)接口(實(shí)現(xiàn)查找功能) |
下面對(duì)各種迭代器接口簡(jiǎn)單介紹一下:
1. Traversable
Traversable接口實(shí)際上不是一個(gè)接口,在實(shí)際寫(xiě)php代碼中不能用。因?yàn)橹挥袃?nèi)部的PHP類(用C寫(xiě)的類)才可以直接實(shí)現(xiàn)Traversable接口。可以說(shuō)這是個(gè)特性級(jí)別的東西。實(shí)際的PHP編程中我們使用Iterator接口或者IteratorAggregate接口來(lái)實(shí)現(xiàn)遍歷。
Traversable {
}
Traversable 接口不能直接實(shí)現(xiàn)(implements).Traversable 重要的一個(gè)用處就是判斷一個(gè)類是否可以遍歷:
if($class instanceof Traversable)
{
//foreach...
}
下面是官方例子:
<?php
if( !is_array( $items ) && !$items instanceof Traversable )
//Throw exception here
?>
2. Iterator
Iterator接口的主要用途是允許一個(gè)類實(shí)現(xiàn)一個(gè)基本的迭代功能,從而使它可以被循環(huán)訪問(wèn),根據(jù)鍵值訪問(wèn)以及回滾。Iterator接口摘要如下:
Iterator extends Traversable
{
//返回當(dāng)前索引游標(biāo)指向的元素
abstract public mixed current(void)
//返回當(dāng)前索引游標(biāo)指向的元素的鍵名
abstract public scalar key(void)
//移動(dòng)當(dāng)前索引游標(biāo)指向下一元素
abstract public void next(void)
//重置索引游標(biāo)的指向第一個(gè)元素
abstract public void rewind(void)
//判斷當(dāng)前索引游標(biāo)指向的是否是一個(gè)元素,常常在調(diào)用 rewind()或 next()使用
abstract public boolean valid(void)
}
外部迭代器接口,實(shí)現(xiàn)該接口的對(duì)象可以迭代自己內(nèi)部的數(shù)據(jù)。
Iterator 的例子這里就不再列舉了,本專題前面部分以后后續(xù)有很多例子,具體請(qǐng)自行查看。
3. IteratorAggregate
又叫聚合式迭代器。創(chuàng)建外部迭代器的接口,其摘要如下:
IteratorAggregate extends Traversable {
//實(shí)現(xiàn)該方法時(shí),必須返回一個(gè)實(shí)現(xiàn)了Iterator接口的類的實(shí)例
abstract public Traversable getIterator ( void )
}
其中g(shù)etIterator 方法返回值必須是能遍歷或?qū)崿F(xiàn)Iterator接口(must be traversable or implement interface Iterator)。SPL還提供了一些專門(mén)用來(lái)與IteratorAggregate接口一起使用的內(nèi)置迭代器。使用這些迭代器意味著只需要實(shí)現(xiàn)一個(gè)方法并實(shí)例化一個(gè)類就可以使對(duì)象可以迭代訪問(wèn)了。 托福答案
實(shí)例:
/**
* @author 簡(jiǎn)明現(xiàn)代魔法 http://www.nowamagic.net
*/
class myData implements IteratorAggregate
{
public $property1 = "公共屬性1";
public $property2 = "公共屬性2";
public $property3 = "公共屬性3";
public function __construct()
{
$this->property4 = "最后一個(gè)公共屬性";
}
public function getIterator()
{
return new ArrayIterator($this);
}
}
$obj = new myData;
foreach ($obj as $key => $value) {
echo "鍵名:{$key} 值:{$value}\n";
}
程序輸出:
鍵名:property1 值:公共屬性1
鍵名:property2 值:公共屬性2
鍵名:property3 值:公共屬性3
鍵名:property4 值:最后一個(gè)公共屬性
4. ArrayAccess
數(shù)組式訪問(wèn)接口。實(shí)現(xiàn)該接口的對(duì)象能像數(shù)組一樣使用:
ArrayAccess {
/* Methods */
abstract public boolean offsetExists ( mixed $offset )
abstract public mixed offsetGet ( mixed $offset )
abstract public void offsetSet ( mixed $offset , mixed $value )
abstract public void offsetUnset ( mixed $offset )
}
- ArrayAccess::offsetExists?— 檢查一個(gè)偏移位置是否存在
- ArrayAccess::offsetGet?— 獲取一個(gè)偏移位置的值
- ArrayAccess::offsetSet?— 設(shè)置一個(gè)偏移位置的值
- ArrayAccess::offsetUnset?— 復(fù)位一個(gè)偏移位置的值
舉個(gè)栗子:
/**
* @author 簡(jiǎn)明現(xiàn)代魔法 http://www.nowamagic.net
*/
class obj implements arrayaccess {
private $container = array();
public function __construct() {
$this->container = array(
"one" => 1,
"two" => 2,
"three" => 3,
);
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->container[] = $value;
} else {
$this->container[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->container[$offset]);
}
public function offsetUnset($offset) {
unset($this->container[$offset]);
}
public function offsetGet($offset) {
return isset($this->container[$offset]) ? $this->container[$offset] : null;
}
}
$obj = new obj;
var_dump(isset($obj["two"]));
var_dump($obj["two"]);
unset($obj["two"]);
var_dump(isset($obj["two"]));
$obj["two"] = "A value";
var_dump($obj["two"]);
$obj[] = 'Append 1';
$obj[] = 'Append 2';
$obj[] = 'Append 3';
print_r($obj);
5. Serializable
序列化接口。實(shí)現(xiàn)該接口的類不能使用__sleep() 和__wakeup().在serialize時(shí)不執(zhí)行__destruct(),在unserialize不執(zhí)行__construct()。
Serializable {
/* Methods */
abstract public string serialize ( void )
abstract public mixed unserialize ( string $serialized )
}
實(shí)現(xiàn)此接口的類將不再支持 __sleep() 和 __wakeup()。不論何時(shí),只要有實(shí)例需要被序列化,serialize 方法都將被調(diào)用。它將不會(huì)調(diào)用 __destruct() 或有其他影響,除非程序化地調(diào)用此方法。當(dāng)數(shù)據(jù)被反序列化時(shí),類將被感知并且調(diào)用合適的 unserialize() 方法而不是調(diào)用 __construct()。如果需要執(zhí)行標(biāo)準(zhǔn)的構(gòu)造器,你應(yīng)該在這個(gè)方法中進(jìn)行處理。 托福改分
- Serializable::serialize?— 對(duì)象的字符串表示
- Serializable::unserialize?— 構(gòu)造對(duì)象
Serializable {
/* Methods */
abstract public string serialize ( void )
abstract public mixed unserialize ( string $serialized )
}
例子:
class obj implements Serializable {
private $data;
public function __construct() {
$this->data = "My private data";
}
public function serialize() {
return serialize($this->data);
}
public function unserialize($data) {
$this->data = unserialize($data);
}
public function getData() {
return $this->data;
}
}
$obj = new obj;
$ser = serialize($obj);
$newobj = unserialize($ser);
var_dump($newobj->getData());
6. Closure
Closure {
/* 方法 */
__construct ( void )
public static Closure bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] )
public Closure bindTo ( object $newthis [, mixed $newscope = 'static' ] )
}
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

