久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

PHP基礎之預定義接口4——ArrayAccess接口

瀏覽:123日期:2022-09-15 10:43:32

提供像訪問數組一樣訪問對象的能力的接口。

接口摘要

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

標簽: PHP
相關文章:
主站蜘蛛池模板: 本溪市| 玛纳斯县| 荥经县| 比如县| 泉州市| 永年县| 龙江县| 阜城县| 海宁市| 满洲里市| 富平县| 马龙县| 兰州市| 津市市| 温宿县| 岗巴县| 宁都县| 衡水市| 从化市| 理塘县| 建瓯市| 汪清县| 界首市| 金川县| 庆云县| 通化县| 大方县| 行唐县| 木兰县| 闻喜县| 囊谦县| 甘肃省| 安庆市| 余干县| 潼关县| 洛川县| 江油市| 阳曲县| 白河县| 邢台县| 唐山市|