PHP基礎(chǔ)之預(yù)定義接口4——ArrayAccess接口
提供像訪問數(shù)組一樣訪問對象的能力的接口。
接口摘要ArrayAccess { /* 方法 */ 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 )}
Example #1 使用范例
<?php 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);?>
以上例程的輸出類似于:
bool(true)int(2)bool(false)string(7) 'A value'obj Object( [container:obj:private] => Array( [one] => 1 [three] => 3 [two] => A value [0] => Append 1 [1] => Append 2 [2] => Append 3))方法列表ArrayAccess::offsetExists?— 檢查一個(gè)偏移位置是否存在ArrayAccess::offsetGet?— 獲取一個(gè)偏移位置的值A(chǔ)rrayAccess::offsetSet?— 設(shè)置一個(gè)偏移位置的值A(chǔ)rrayAccess::offsetUnset?— 復(fù)位一個(gè)偏移位置的值
相關(guān)文章:
1. java實(shí)現(xiàn)2048小游戲(含注釋)2. 詳解CSS偽元素的妙用單標(biāo)簽之美3. CSS自定義滾動(dòng)條樣式案例詳解4. Ajax實(shí)現(xiàn)表格中信息不刷新頁面進(jìn)行更新數(shù)據(jù)5. Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)6. UDDI FAQs7. PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)之類屬性與類常量實(shí)現(xiàn)方法分析8. HTML <!DOCTYPE> 標(biāo)簽9. python 批量下載bilibili視頻的gui程序10. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法
