国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

PHP基礎(chǔ)之預(yù)定義接口4——ArrayAccess接口

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

提供像訪問數(shù)組一樣訪問對(duì)象的能力的接口。

接口摘要

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è)偏移位置的值

標(biāo)簽: PHP
相關(guān)文章:
主站蜘蛛池模板: 绵阳市| 工布江达县| 高平市| 深水埗区| 新营市| 格尔木市| 米林县| 西吉县| 齐齐哈尔市| 东乡| 开化县| 沂水县| 长乐市| 信丰县| 柏乡县| 澎湖县| 彰武县| 张北县| 武宣县| 西藏| 乌拉特前旗| 遵化市| 罗源县| 大悟县| 八宿县| 西吉县| 遂宁市| 高邑县| 苍梧县| 水富县| 上林县| 开阳县| 革吉县| 平乐县| 卓尼县| 乌拉特中旗| 崇阳县| 师宗县| 平山县| 漳浦县| 远安县|