迭代器或类的接口。
Iterator extends Traversable
{
//返回当前索引游标指向的元素
abstract public mixed current(void)
//返回当前索引游标指向的元素的键名
abstract public scalar key(void)
//移动当前索引游标指向下一元素
abstract public void next(void)
//重置索引游标的指向第一个元素
abstract public void rewind(void)
//判断当前索引游标指向的是否是一个元素,常常在调用 rewind()或 next()使用
abstract public boolean valid(void)
}
scalar 标准变量 整型、字符串、浮点型等
$a="hello word";
$obj=(object)$a;
echo $obj->scalar;
//输出 "hello word"
class myIterator implements Iterator {
private $position = 0;
private $array = array(
"firstelement",
"secondelement",
"lastelement",
);
public function __construct() {
$this->position = 0;
}
function rewind() {
// var_dump(__METHOD__);
$this->position = 0;
}
function current() {
// var_dump(__METHOD__);
return $this->array[$this->position];
}
function key() {
// var_dump(__METHOD__);
return $this->position;
}
function next() {
// var_dump(__METHOD__);
++$this->position;
}
function valid() {
// var_dump(__METHOD__);
return isset($this->array[$this->position]);
}
}
$it = new myIterator;
foreach($it as $key => $value) {
echo "{$key}|{$value}";
echo "\n";
}
输出结果:
0|firstelement
1|secondelement
2|lastelement
Traversable 预定义接口
Traversable {
}
作用:检测一个类是否可以使用 foreach 进行遍历的接口。
php代码中不能用。只有内部的PHP类(用C写的类)才可以直接实现Traversable接口
IteratorAggregate
预定义接口(对Iterator的委托)
class myDataArray implements IteratorAggregate {
private $_d = array('a','b','c','d');
public function __construct() {
$this->_d[] = "last property";
}
public function getIterator() {
return new ArrayIterator($this->_d);
}
}
$obj = new myDataArray;
foreach($obj as $key => $value) {
var_dump($key, $value);
echo "\n";
}
ArrayAccess
PHP的ArrayAccess接口 像数组一样来访问你的PHP对象
class Test implements ArrayAccess {
private $elements;
public function offsetExists($offset){
return isset($this->elements[$offset]);
}
public function offsetSet($offset, $value){
$this->elements[$offset] = $value;
}
public function offsetGet($offset){
return $this->elements[$offset];
}
public function offsetUnset($offset){
unset($this->elements[$offset]);
}
}
$test = new Test();
$test['test'] = 'test';//自动调用offsetSet
if(isset($test['test']))//自动调用offsetExists
{
echo $test['test'];//自动调用offsetGet
echo '<br />';
unset($test['test']);//自动调用offsetUnset
var_dump($test['test']);
}
Lararel中常常使用比如$this['config']
SeekableIterator 预定义接口
对接口Iterator的扩展
在接口中可以 seek
SeekableIterator extends Iterator {
/* 方法 */
abstract public void seek ( int $position )
/* 继承的方法 */
abstract public mixed Iterator::current ( void )
abstract public scalar Iterator::key ( void )
abstract public void Iterator::next ( void )
abstract public void Iterator::rewind ( void )
abstract public boolean Iterator::valid ( void )
Serializable
扩展接口,对类序列化就是对类内对应方法数据序列反序列化
class obj implements Serializable {
private $data;
public function __construct() {
$this->data = ["My private data"];
}
public function serialize() {
return serialize($this->data);
}
public function unserialize($data) {
$this->data = unserialize($data);
}
public function getData() {
return $this->data;
}
}
$obj = new obj;
$ser = serialize($obj);
$newobj = unserialize($ser);
var_dump($newobj->getData());
Countable
能够对对象实现count操作
class Basket implements Countable{
private $fruits =array('apple','banna','pear','orange','watermelon');
public function count(){
return count($this->fruits);
}
}
$basket = new Basket();
var_dump(count($basket));