在php中有好几个预定义的接口,比较常用的四个接口(iteratoraggregate(聚合式aggregate迭代器iterator)、countable、arrayaccess、iterator)分别给大家详细介绍下。
iteratoraggregate(聚合式aggregate迭代器iterator)接口
iteratoraggregate extends traversable {
abstract public traversable getiterator(void)
}
这个接口实现了一个功能——创建外部迭代器,具体怎么理解呢,当我们使用foreach对对象进行便遍历的时候,如果没有继承iteratoraggregate接口,遍历的是对象中所有的public属性(只能是public $var这种形式)。要是继承了iteratoraggregate,会使用类中实现的getiterator方法返回的对象,这里要注意返回的一定要是一个traversable对象或者扩展自traversable的对象,否则会抛出异常
//看个例子
class my{
private $_data = [
'a' => '燕睿涛',
'b' => 'yanruitao',
'c' => 'lulu',
];
public function getiterator()
{
return new arrayiterator($this->_data);
}
}
$obj = new my;
foreach ($obj as $key => $value) {
echo "$key => $valuen";
}
//输出结果为空
class my implements iteratoraggregate {
private $_data = [
'a' => '燕睿涛',
'b' => 'yanruitao',
'c' => 'lulu',
];
public function getiterator()
{
return new arrayiterator($this->_data);
}
}
$obj = new my;
foreach ($obj as $key => $value) {
echo "$key => $valuen";
}
//结果:
a => 燕睿涛
b => yanruitao
c => lulu
countable接口
countable {
abstract public int count(void)
}
这个接口用于统计对象的数量,具体怎么理解呢,当我们对一个对象调用count的时候,如果函数没有继承countable将一直返回1,如果继承了countable会返回所实现的count方法所返回的数字,看看下面的例子:
class countme
{
protected $_mycount = 3;
public function count()
{
return $this->_mycount;
}
}
$countable = new countme();
echo count($countable);
//返回1
class countme implements countable
{
protected $_mycount = 3;
public function count()
{
return $this->_mycount;
}
}
$countable = new countme();
echo count($countable);
//返回3
arrayaccess接口
arrayaccess {
abstract public boolean offsetexists(mixed $offset)
abstract public mixed offsetget(mixed $offset)
public void offsetset(mixed $offset, mixed $value)
public void offsetunset(mixed $offset)
}
class countme
{
protected $_mycount = 3;
public function count()
{
return $this->_mycount;
}
}
$countable = new countme();
echo count($countable);
//返回1
class countme implements countable
{
protected $_mycount = 3;
public function count()
{
return $this->_mycount;
}
}
$countable = new countme();
echo count($countable);
//返回3
arrayaccess接口
arrayaccess {
abstract public boolean offsetexists(mixed $offset)
abstract public mixed offsetget(mixed $offset)
public void offsetset(mixed $offset, mixed $value)
public void offsetunset(mixed $offset)
}
这个接口的作用是让我们可以像访问数组一样访问对象,这个怎么说好呢,我猜其实就是php在词法分析的时候如果碰到了数组的方式使用对象,就回去对象中查找是否有实现arrayaccess如果有的话,进行对应的操作(set、unset、isset、get),这样我们就可以在类里面放置一个array,让类实现数组方式的基本操作,下面看个例子:
class myobj
{
}
$obj = new myobj;
$obj['name'];
//fatal error: cannot use object of type myobj as array in
class myobj implements arrayaccess
{
public function offsetset($offset, $value)
{
echo "offsetset : {$offset} => {$value}n";
}
public function offsetexists($offset)
{
echo "offsetexists : {$offset}n";
}
public function offsetunset($offset)
{
echo "offsetunset : {$offset}n";
}
public function offsetget($offset)
{
echo "offsetget : {$offset}n";
}
}
$obj = new myobj;
$obj[1] = '燕睿涛';
isset($obj['name']);
unset($obj['name']);
$obj['yrt'];
//输出结果:
offsetset : 1 => 燕睿涛
offsetexists : name
offsetunset : name
offsetget : yrt
class myobj implements arrayaccess
{
private $_data = [];
public function offsetset($offset, $value)
{
$this->_data[$offset] = $value;
}
public function offsetexists($offset)
{
return isset($this->_data[$offset]);
}
public function offsetunset($offset)
{
unset($this->_data[$offset]);
}
public function offsetget($offset)
{
return $this->_data[$offset];
}
}
$obj = new myobj;
$obj['yrt'] = '燕睿涛';
var_dump($obj['yrt']);
var_dump(isset($obj['yrt']));
unset($obj['yrt']);
var_dump(isset($obj['yrt']));
var_dump($obj['yrt']);
//输出:
string(9) "燕睿涛"
bool(true)
bool(false)
notice: undefined index: yrt //最后一个会报出notice
上面的对象只能是基本的数组操作,连遍历都不行,结合之前的iteratoraggregate可以进行foreach:
class myobj implements arrayaccess, iteratoraggregate
{
private $_data = [];
public function getiterator()
{
return new arrayiterator($this->_data);
}
......
}
$obj = new myobj;
$obj['yrt'] = '燕睿涛';
$obj[1] = '燕睿涛';
$obj['name'] = '燕睿涛';
$obj['age'] = 23;
foreach ($obj as $key => $value) {
echo "{$key} => {$value}n";
}
//输出:
yrt => 燕睿涛
1 => 燕睿涛
name => 燕睿涛
age => 23
iterator接口:
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)
}
可在内部迭代自己的外部迭代器或类的接口,这是官方文档给出的解释,看着还是不好理解,其实我感觉这个接口实现的功能和trratoraggregate(文档:创建外部迭代器接口,接口直接返回一个迭代器)类似,不过这个在类的定义里面自己实现了,看个例子:
class myobj implements iterator{
private $_data = [];
public function __construct(array $arr)
{
$this->_data = $arr;
}
public function current()
{
return current($this->_data);
}
public function key()
{
return key($this->_data);
}
public function next()
{
next($this->_data);
}
public function rewind()
{
reset($this->_data);
}
public function valid()
{
return $this->key() !== null;
}
}
$t = [
'yrt' => '燕睿涛',
'name' => '燕睿涛',
false,
'燕睿涛'
];
$obj = new myobj($t);
foreach ($obj as $key => $value) {
echo "{$key} => ".var_export($value, true)."n";
}
//输出:
yrt => '燕睿涛'
name => '燕睿涛'
0 => false
1 => '燕睿涛'
上面这个参考了鸟哥的一篇文章关于一笔试题(iterator模式),不过鸟哥的那个判断valid有点瑕疵,当碰到值北来就是false的时候就会截断
总结
说了这么多好像还是没有体会到他们的用处,建议看看yii2的源码,源码里面大量使用了这些东西,看了之后,你会慢慢觉得“哦~好像还真是挺有用的。。。。”
以上就是本文全部介绍,希望大家喜欢。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!