您好,登錄后才能下訂單哦!
接口摘要 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 ) }
舉個例子
class Test implements ArrayAccess { private $testData = []; public function offsetExists($offset) { echo 'call ' . __METHOD__ . "\r\n"; return isset($this->testData[$offset]); } public function offsetGet($offset) { echo 'call ' . __METHOD__ . "\r\n"; return $this->testData[$offset]; } public function offsetSet($offset, $value) { echo 'call ' . __METHOD__ . "\r\n"; return $this->testData[$offset] = $value; } public function offsetUnset($offset) { echo 'call ' . __METHOD__ . "\r\n"; unset($this->testData[$offset]); } }
$obj = new Test();
if (!isset($obj['name'])) { //call Test::offsetExists
$obj['name'] = 'zhangsan'; //call Test::offsetSet
}
echo $obj['name'] . "\r\n"; //call Test::offsetGet
var_dump($obj);
$obj['age'] = 18; //call Test::offsetSet
echo $obj['age'] . "\r\n"; //call Test::offsetGet
unset($obj['address']); //call Test::offsetUnset
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。