Iterator
Iterator接口的作用是允许对象以自己的方式迭代内部的数据,从而使它可以被循环访问,Iterator接口摘要如下:
interface Iterator extends Traversable {
//返回当前索引游标指向的元素
abstract public mixed current ( void )
//返回当前索引游标指向的键
abstract public scalar key ( void )
//移动当前索引游标到下一元素
abstract public void next ( void )
//重置索引游标
abstract public void rewind ( void )
//判断当前索引游标指向的元素是否有效
abstract public boolean valid ( void )
}
重点理解
<?php
class MyIterator implements Iterator
{
protected $data;
public function __construct($data = [])
{
if(!empty($data))
$this->setData($data);
}
public function current()
{
//echo "获取当前数组元素";
return current($this->data);
}
public function next()
{
//echo "数组指针指向下一个元素";
next($this->data);
}
public function key()
{
//echo "获取当前键名";
return key($this->data);;
}
public function valid()
{
//echo "检查有效性";
return $this->current() !== false?true:false;
}
public function rewind()
{
//重置数组指针
reset($this->data);
}
/**
* @return mixed
*/
public function getData()
{
return $this->data;
}
/**
* @param mixed $data
*/
public function setData($data)
{
$this->data = $data;
}
}
$data = [
'name' => '谭勇',
'age' => 22,
'mobile'=> '15683008628'
];
$_iter = new MyIterator($data);
foreach($_iter as $k=>$v)
{
echo "<p>{$k}=>{$v}</p>";
}
运行上述程序将会显示:
name=>谭勇
age=>22
mobile=>15683008628
ArrayAccess
ArrayAccess 的作用是使得你的对象可以像数组一样可以被访问。应该说 ArrayAccess 在PHP5中才开始有的,PHP5中加入了很多新的特性,当然也使类的重载也加强了,PHP5 中添加了一系列接口,这些接口和实现的 Class 统称为 SPL。
ArrayAccess 这个接口定义了4个必须要实现的方法:
/**
* Interface to provide accessing objects as arrays.
* @link http://php.net/manual/en/class.arrayaccess.php
*/
interface ArrayAccess {
/**
* 检查偏移位置是否存在
* (PHP 5 >= 5.0.0)<br/>
* Whether a offset exists
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset <p>
* An offset to check for.
* </p>
* @return boolean true on success or false on failure.
* </p>
* <p>
* The return value will be casted to boolean if non-boolean was returned.
*/
public function offsetExists($offset);
/**
* 获取一个偏移位置的值
* (PHP 5 >= 5.0.0)<br/>
* Offset to retrieve
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset <p>
* The offset to retrieve.
* </p>
* @return mixed Can return all value types.
*/
public function offsetGet($offset);
/**
* 设置一个偏移位置的值
* (PHP 5 >= 5.0.0)<br/>
* Offset to set
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* The value to set.
* </p>
* @return void
*/
public function offsetSet($offset, $value);
/**
* 复位一个偏移位置的值
* (PHP 5 >= 5.0.0)<br/>
* Offset to unset
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset <p>
* The offset to unset.
* </p>
* @return void
*/
public function offsetUnset($offset);
}
使用案例
<?php
class MyArray implements ArrayAccess
{
protected $data;
public function __construct($data = [])
{
if(!empty($data))
$this->setData($data);
}
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
public function offsetGet($offset)
{
return $this->offsetExists($offset)?$this->data[$offset]:'';
}
public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}
public function offsetUnset($offset)
{
if($this->offsetExists($offset))
unset($this->data[$offset]);
}
/**
* @return mixed
*/
public function getData()
{
return $this->data;
}
/**
* @param mixed $data
*/
public function setData($data)
{
$this->data = $data;
}
}
$data = [
'name' => '谭勇'
];
$_arr = new MyArray($data);
$_arr['age'] = 25;
$_arr['mobile'] = '15683008628';
var_dump($_arr);
echo "<p>age : " . $_arr['age'] . "</p>";
echo "<p>name : " . $_arr['name'] . "</p>";
echo "<p>mobile : " . $_arr['mobile'] . "</p>";
上述代码运行结果
object(MyArray)#1 (1) { ["data":protected]=> array(3) { ["name"]=> string(6) "谭勇" ["age"]=> int(25) ["mobile"]=> string(11) "15683008628" } }
age : 25
name : 谭勇
mobile : 15683008628
Iterator和ArrayAccess联合使用
<?php
class MyObj implements Iterator,ArrayAccess
{
protected $data;
public function __construct($data = [])
{
if(!empty($data))
$this->setData($data);
}
public function current()
{
//echo "获取当前数组元素";
return current($this->data);
}
public function next()
{
//echo "数组指针指向下一个元素";
next($this->data);
}
public function key()
{
//echo "获取当前键名";
return key($this->data);;
}
public function valid()
{
//echo "检查有效性";
return $this->current() !== false?true:false;
}
public function rewind()
{
//重置数组指针
reset($this->data);
}
public function offsetExists($offset)
{
return isset($this->data[$offset]);
}
public function offsetGet($offset)
{
return $this->offsetExists($offset)?$this->data[$offset]:'';
}
public function offsetSet($offset, $value)
{
$this->data[$offset] = $value;
}
public function offsetUnset($offset)
{
if($this->offsetExists($offset))
unset($this->data[$offset]);
}
/**
* @return mixed
*/
public function getData()
{
return $this->data;
}
/**
* @param mixed $data
*/
public function setData($data)
{
$this->data = $data;
}
}
$data = [
'name' => '谭勇',
'age' => 22,
'mobile'=> '15683008628'
];
$obj = new MyObj($data);
$obj['desc'] = "php 开发工程师";
$obj['info'] = "123";
foreach($obj as $k=>$v)
{
echo "<p>{$k}=>{$v}</p>";
}
var_dump($obj);
上述代码执行结果
name=>谭勇
age=>22
mobile=>15683008628
desc=>php 开发工程师
info=>123
object(MyObj)#1 (1) { ["data":protected]=> array(5) { ["name"]=> string(6) "谭勇" ["age"]=> int(22) ["mobile"]=> string(11) "15683008628" ["desc"]=> string(19) "php 开发工程师" ["info"]=> string(3) "123" } }