複習PHP-語言參考-預定義介面

來源:互聯網
上載者:User

標籤:current   檢測   介面   元素   

1.Traversable

他是一個遍曆介面規範

注意:發現一個有用的函數get_declared_classes,可以以數組形式顯示當前指令碼下所有已經定義的類名

2.Iterator

Iterator迭代器繼承自Traversable,是一種遍曆對象內容的對象。

你可以自己寫一個子類繼承自它,並寫上具體遍曆的方法。

Iterator包含:current(返回當前元素),key(當前鍵),next(下一個元素),rewind(返回至初始元素),valid(檢測當前元素是否存在)五種方法。

3.IteratorAggregate

與Iterator不同的是 通過這個介面可以用此基類的getIterator方法返回一個外部迭代器對象用於遍曆子類對象內容。

4.arrayaccess

可以通過這個數組式提供者動態添加修改和訪問數組的屬性

  • ArrayAccess::offsetExists — 檢查一個位移位置是否存在
  • ArrayAccess::offsetGet — 擷取一個位移位置的值
  • ArrayAccess::offsetSet — 設定一個位移位置的值
  • ArrayAccess::offsetUnset — 複位一個位移位置的值

 

挺好的一個東西,和__get  __set 類似。

例:

<?php
class obj implements ArrayAccess
{
    private $array = [];
    /**
     * @param offset
     */
    public function offsetExists ($offset) {
        return isset($array[$offset]);
    }   
    /**
     * @param offset
     */
    public function offsetGet ($offset) {
        return $this->offsetExists($offset)?$this->array[$offset]:NULL;
    }   
    /**
     * @param offset
     * @param value
     */
    public function offsetSet ($offset, $value) {
        if(!is_null($offset))
        {
            $this->array[$offset] = $value;
        }
        else{
            $this->array[] = $value;
        }
    }   
    /**
     * @param offset
     */
    public function offsetUnset ($offset) {
        unset($this->array[$offset]);
    }
}
$obj = new obj;
$obj[‘a‘] = 5;
$obj[‘b‘] = "string";
$obj[‘c‘] = 6.7;
$obj[‘d‘] = [2,3,4];
$obj[] = "haha";
unset($obj[‘a‘]);
print_r($obj);
?>

5.序列化介面

自訂序列化和還原序列化規則。

見下列,序列化的介面可以重載系統內建函數serialize和unserialize。是否可以用此來加密對象?

<?php
class a implements Serializable
{
    private $data = "abcde";
    public function serialize () {
        echo __METHOD__;
        return serialize($this->data);   
    }
    public function unserialize ($serialized) {
        echo __METHOD__;
        return unserialize($this->data);
    }
    public function getData()
    {
        return $this->data;
    }   
}
$obj = new a;
$nobj = serialize($obj);
echo $nobj;
?>

6.Closure類

它和閉包函數非常相似

例:

<?php
//1.匿名函數
$a = function ($param){echo $param;};
//2.簡單的閉包
//2.1
function p()
{
    $f = function($str)
    {
        echo $str;
    };
    $f("something");
}
//2.2
function d()
{
    return function ($str){echo $str;};
}
//2.3 注意這裡的$f是一個函數
function e($f)
{
    $f("something");
}
//2.4
e(function($str){echo $str;});
//3.串連閉包和外部變數的關鍵詞 use 這在之前寫購物車類時已經用過了
function get()
{
    $rmb = 1;
    $func = function() use (&$rmb)
    {
        ++$rmb;
        echo $rmb;
    };
    $func(); // print 2;
    //或者return $func;
    return $func;   
}
?>

發現一種新的寫法:靜態函數

<?php
$a = static function()
{
    echo "good";
};
$a();
?>

注意:closure的bind和bindTo跳過了 因為只支援5.4 所以實際運用中不多見,暫時不看了。

本章主要是迭代器的理解。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.