2012-07-04 78 views
1

我試圖瞭解背後ArrayAccess接口界面的想法,PHP,SPL,ArrayAccess接口接口

我不明白每一種方法是一下,如果這些方法(函數)是「內置」的功能和ArrayAccess接口接口(也「內置」)只是「確保」我要實現那些「內置」方法(功能)

我想了解每個thoes函數是在用我們的代碼「幕後」 。

function offsetSet($offset, $value); 
function offsetGet($offset); 
function offsetUnset($offset); 
function offsetExists($offset); 

如果我明白了ArrayAccess是一個內置的接口,包含密封件實現,當我們執行他們,我們只實現內置函數thoes引用,我會很高興,如果有人可以幫助我得到這個權利。

+1

這個課程在這裏有很好的文檔http://php.net/manual/en/class.arrayaccess.php – Ziumin

回答

6

如果您實現了該接口,那麼該對象就像一個數組。例如,如果$foo是實現的類的實例ArrayAccess

$foo['bar'] = 42 調用offsetSet('bar', 42)

echo $foo['bar']調用offsetGet('bar')

unset($foo['bar'])來電offsetUnset('bar')

isset($foo['bar'])來電offsetExists('bar')

你從不顯式調用函數offset *自己。當你以數組的形式訪問對象時,它會隱式發生。

+0

非常感謝你Matthew,如果你確認我對這個主題的理解,我會非常高興的。是一個「內置接口」這個接口,包含這些函數的簽名,這些函數引用了一個「內置」函數,當我們以數組的形式訪問對象時會自動響應,並且非常感謝您的回答。手冊並不明白這個界面,如果你可以請確認,祝你有個美好的一天馬修。 –

+0

該接口內置了這四個函數簽名,但PHP程序員必須實際執行它們。在像訪問數組時一樣自動調用偏移量*函數的行爲也是內置的...您無法使用自己的接口進行操作。 (有幾個內置接口在實現時類似地影響核心PHP功能。) – Matthew

+0

謝謝馬修,如果我得到這個權利是唯一的函數和參數簽名,並自動調用偏移*的行爲,但請告訴我,這種行爲是它只是說當你訪問數組調用偏移*,什麼我說的是這種行爲只是指函數名稱?什麼意思是我的責任是將功能設置爲我想要的功能,並再次感謝。 –

0

雖然比較ArrayAccessSimpleXMLElement(內部類沒有實現它),我也很好奇。該界面已經在手冊中有詳細記錄,所以我想強調一些與偏移類型有關的特定差異。

但首先一個樣板示例實現執行ArrayAccess給輸出類的訪問時:

/** 
* ArrayAccess Example 
*/ 
class ExampleArrayLikeAccess implements ArrayAccess 
{ 

    /** 
    * Whether a offset exists 
    * 
    * @link http://php.net/manual/en/arrayaccess.offsetexists.php 
    * @param mixed $offset - An offset to check for. 
    * @return boolean true on success or false on failure. 
    * 
    * The return value will be casted to boolean if non-boolean was returned. 
    */ 
    public function offsetExists($offset) { 
     echo " - offsetExists(", $this->varString($offset),")\n"; 
    } 

    /** 
    * Offset to retrieve 
    * 
    * @link http://php.net/manual/en/arrayaccess.offsetget.php 
    * @param mixed $offset The offset to retrieve. 
    * @return mixed Can return all value types. 
    */ 
    public function offsetGet($offset) { 
     echo " - offsetGet(", $this->varString($offset),")\n"; 
    } 

    /** 
    * Offset to set 
    * 
    * @link http://php.net/manual/en/arrayaccess.offsetset.php 
    * @param mixed $offset The offset to assign the value to. 
    * @param mixed $value The value to set. 
    * @return void 
    */ 
    public function offsetSet($offset, $value) { 
     echo " - offsetSet(", $this->varString($offset), ", ", $this->varString($value), ")\n"; 
    } 

    /** 
    * Offset to unset 
    * @link http://php.net/manual/en/arrayaccess.offsetunset.php 
    * @param mixed $offset The offset to unset. 
    * @return void 
    */ 
    public function offsetUnset($offset) { 
     echo " - offsetUnset(", $this->varString($offset),")\n"; 
    } 

    /** 
    * helper to give a variable dump in form of a string 
    */ 
    private function varString($var) { 
     ob_start(); 
     var_dump($var); 
     return trim(strtr(ob_get_clean(), ["\n" => '', "\r" => '']), ' {}'); 
    } 

} 

運行與它的一些用法,例子。我以評論的形式留下了筆記。它應該是非常自我解釋的:

$like = new ExampleArrayLikeAccess(); 


/* offsetExists */ 

// indexes/keys that behave similar to PHP arrays: 

isset($like[1]); # integer stay integer 
# offsetExists(int(1)) 

isset($like['1']); # string like an integer - converted to integer 
# offsetExists(int(1)) 

isset($like['01']); # string unlike an integer - stays string 
# offsetExists(string(2) "01") 

isset($like[TRUE]); # booleans are converted to integer 
# offsetExists(bool(true)) 

// indexes/keys that differ to PHP arrays: 

isset($like[1.1]);  # a float stays a float (double) 
# offsetExists(double(1.1)) 

isset($like[NULL]); # NULL stays NULL 
# offsetExists(NULL) 

isset($like[array()]); # array stays array 
# offsetExists(array(0)) 

isset($like[$like]); # object stays object 
# offsetExists(class SxeLikeAccess#2 (0)) 


/* offsetGet */ 

// indexes/keys behave the same as with offsetExists: 
$like[1]; # offsetGet(int(1)) 
$like['1']; # offsetGet(int(1)) 
$like['01']; # offsetGet(string(2) "01") 
// ... 


/* offsetSet */ 

$like[1] = 'value'; # index/key behaves the same as with offsetExists 
# offsetSet(int(1), string(5) "value") 

$like[] = 'value';  # index/key is NULL 
# offsetSet(NULL, string(5) "value") 

$like[NULL] = 'value'; # index/key is NULL 
# offsetSet(NULL, string(5) "value") 


/* offsetUnset */ 
unset($like[1]);  # index/key behaves the same as with offsetExists 
unset($like[NULL]); # same for NULL 

與標準PHP數組的主要區別在於,您不僅可以使用整數和字符串作爲偏移量。