亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

PHP Predefined Interfaces 預定義接口

系統 2159 0

SPL提供了6個迭代器接口:

Traversable 遍歷接口(檢測一個類是否可以使用?foreach?進行遍歷的接口)
Iterator 迭代器接口(可在內部迭代自己的外部迭代器或類的接口)
IteratorAggregate 聚合式迭代器接口(創建外部迭代器的接口)
OuterIterator 迭代器嵌套接口(將一個或多個迭代器包裹在另一個迭代器中)
RecursiveIterator 遞歸迭代訪問接口(提供遞歸訪問功能)
SeekableIterator 可索引迭代訪問接口(實現查找功能)

下面對各種迭代器接口簡單介紹一下:

1. Traversable

Traversable接口實際上不是一個接口,在實際寫php代碼中不能用。因為只有內部的PHP類(用C寫的類)才可以直接實現Traversable接口。可以說這是個特性級別的東西。實際的PHP編程中我們使用Iterator接口或者IteratorAggregate接口來實現遍歷。

    Traversable {

}


  

Traversable 接口不能直接實現(implements).Traversable 重要的一個用處就是判斷一個類是否可以遍歷:

    if($class instanceof Traversable)

{

     //foreach...

}


  

下面是官方例子:

    <?php

    if( !is_array( $items ) && !$items instanceof Traversable )

        //Throw exception here

?>


  

2. Iterator

Iterator接口的主要用途是允許一個類實現一個基本的迭代功能,從而使它可以被循環訪問,根據鍵值訪問以及回滾。Iterator接口摘要如下:

    Iterator extends Traversable  

{  

    //返回當前索引游標指向的元素  

    abstract public mixed current(void)  

    //返回當前索引游標指向的元素的鍵名  

    abstract public scalar key(void)  

    //移動當前索引游標指向下一元素  

    abstract public void next(void)  

    //重置索引游標的指向第一個元素  

    abstract public void rewind(void)  

    //判斷當前索引游標指向的是否是一個元素,常常在調用 rewind()或 next()使用  

    abstract public boolean valid(void)  

} 


  

外部迭代器接口,實現該接口的對象可以迭代自己內部的數據。

Iterator 的例子這里就不再列舉了,本專題前面部分以后后續有很多例子,具體請自行查看。

3. IteratorAggregate

又叫聚合式迭代器。創建外部迭代器的接口,其摘要如下:

    IteratorAggregate extends Traversable {  

	//實現該方法時,必須返回一個實現了Iterator接口的類的實例  

	abstract public Traversable getIterator ( void )  

} 


  

其中getIterator 方法返回值必須是能遍歷或實現Iterator接口(must be traversable or implement interface Iterator)。SPL還提供了一些專門用來與IteratorAggregate接口一起使用的內置迭代器。使用這些迭代器意味著只需要實現一個方法并實例化一個類就可以使對象可以迭代訪問了。 托福答案

實例:

    /**

* @author 簡明現代魔法 http://www.nowamagic.net

*/

class myData implements IteratorAggregate  

{  

    public $property1 = "公共屬性1";  

    public $property2 = "公共屬性2";  

    public $property3 = "公共屬性3";  

  

    public function __construct()  

    {  

        $this->property4 = "最后一個公共屬性";  

    }  

  

    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  值:最后一個公共屬性


  

4. ArrayAccess

數組式訪問接口。實現該接口的對象能像數組一樣使用:

    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?— 檢查一個偏移位置是否存在
  • ArrayAccess::offsetGet?— 獲取一個偏移位置的值
  • ArrayAccess::offsetSet?— 設置一個偏移位置的值
  • ArrayAccess::offsetUnset?— 復位一個偏移位置的值

舉個栗子:

    /**

* @author 簡明現代魔法 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

序列化接口。實現該接口的類不能使用__sleep() 和__wakeup().在serialize時不執行__destruct(),在unserialize不執行__construct()。

    Serializable {

	/* Methods */

	abstract public string serialize ( void )

	abstract public mixed unserialize ( string $serialized )

}


  

實現此接口的類將不再支持 __sleep() 和 __wakeup()。不論何時,只要有實例需要被序列化,serialize 方法都將被調用。它將不會調用 __destruct() 或有其他影響,除非程序化地調用此方法。當數據被反序列化時,類將被感知并且調用合適的 unserialize() 方法而不是調用 __construct()。如果需要執行標準的構造器,你應該在這個方法中進行處理。 托福改分

  • Serializable::serialize?— 對象的字符串表示
  • Serializable::unserialize?— 構造對象
    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' ] )

}


  

PHP Predefined Interfaces 預定義接口


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 啪啪综合网 | 99爱视频 | 欧美成人免费xxx大片 | 狠狠色狠狠色综合系列 | 久久99国产精品久久欧美 | 成人一级黄色片 | 久久91亚洲精品久久91综合 | 国产精品手机在线观看 | 天天色天天舔 | 免费国产精成人品 | 国产一区在线视频观看 | 一级毛片a女人刺激视频免费 | 久久精品资源站 | 亚洲综合成人网在线观看 | 中文一级片 | 日韩a无吗一区二区三区 | 久久99精品久久久久久首页 | 久久99精品久久久66 | 成人爱爱爱欧美日本视频 | 色婷婷天天综合在线 | 99激情网 | 毛片视 | 欧美一级在线播放 | 欧美成人精品福利在线视频 | 婷婷视频在线 | 免费国产a国产片高清不卡 免费国产阿v视频在线观看 | 国产极品嫩模大尺度福利视频 | 97国内免费久久久久久久久久 | 欧美在线香蕉在线现视频 | 亚洲四虎影院 | 特黄特级高清免费视频毛片 | 热九九精品| 一区二区三区在线 | 欧 | 国产成+人欧美+综合在线观看 | 九九热伊人 | 高清人人天天夜夜曰狠狠狠狠 | 日韩欧美国产综合 | 亚洲人成影院午夜网站 | 日本不卡视频在线视频观看 | 成人夜夜| 美女黄频 |