时间:2021-07-01 10:21:17 帮助过:4人阅读
fruits[$this->position]; } //返回当前的键 public function key(){ return $this->position } //下移一个元素 public function next(){ ++$this->position; } //移动到第一个元素 public function rewind(){ $this->position = 0; } //判断后续是否还有元素 public function valid(){ return isset($this->fruits[$this->position+1]); } }
使对象中的数据可以像数组一样访问
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']); echo $obj['two']; $obj[] = 'Append 1'; $obj[] = 'Append 2'; $obj[] = 'Append 3'; var_dump($obj);
使对象可以对属性进行计数
fruits); } } $basket = new Basket(); echo count($basket);
以上就介绍了24php中使用Iterator,ArrayAccess和Countable,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。