PHP中预定义的6种接口介绍


Posted in PHP onMay 12, 2015

PHP预定义了6个接口介绍如下:

1.Traversable遍历接口

呵呵!其实它不是一个在PHP中可以使用的接口,内部类才可使用,它有一个用途就是检测一个类是否可以遍历。

if($class instanceof Traversable) {
  //foreach
}

2.Iterator迭代器接口

接口摘要:

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) 
}

以上可以让一个类实现一个基本的迭代功能,如下可以看到迭代的调用顺序:

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 ) {
  var_dump ( $key , $value );
  echo "\n" ;
}

3.IteratorAggregate聚合式迭代器接口

接口摘要:

IteratorAggregate extends Traversable {
 
//获取外部迭代器
abstract public Traversable getIterator ( void )
}

getIterator是一个Iterator或Traversable接口的类的一个实例。如下获取外部迭代器实现迭代访问。

class myData implements IteratorAggregate {
  public $property1 = "Public property one" ;
  public $property2 = "Public property two" ;
  public $property3 = "Public property three" ;
 
  public function __construct () {
    $this -> property4 = "last property" ;
  }
 
  
  public function getIterator () {
    return new ArrayIterator ( $this );
  }
}
 
$obj = new myData ;
 
foreach( $obj as $key => $value ) {
  var_dump ( $key , $value );
  echo "\n" ;
}

4.ArrayAccess数组式访问接口

接口摘要:

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 obj implements arrayaccess {
  private $container = array();
  public function __construct () {
    $this -> 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" ]);
$obj [] = 'Append 1' ;
$obj [] = 'Append 2' ;
$obj [] = 'Append 3' ;
print_r ( $obj );

5.Serializable序列化接口

接口摘要:

Serializable {
 
  /* 方法 */
  abstract public string serialize ( void ) //对象的字符串表示
  abstract public mixed unserialize ( string $serialized ) // 构造对象
}

实现该接口的类不再支持__sleep()和__wakeup()。使用很简单,只要序列化对象时serialize方法会被调用,当反序列化时,unserialize方法被调用。

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 );
print_r($ser);
$newobj = unserialize ( $ser );
print_r($newobj);

6.Closure
接口摘要:

Closure {
  /* 方法 */
  __construct ( void ) //用于禁止实例化的构造函数
  public static Closure bind ( Closure $closure , object $newthis [, mixed $newscope = 'static' ] ) //复制一个闭包,绑定指定的$this对象和类作用域。
  public Closure bindTo ( object $newthis [, mixed $newscope = 'static' ] ) //复制当前闭包对象,绑定指定的$this对象和类作用域。
}
class A {
  private static $sfoo = 1 ;
  private $ifoo = 2 ;
}
 $cl1 = static function() {
  return A :: $sfoo ;
};
 $cl2 = function() {
  return $this -> ifoo ;
};
 
 $bcl1 = Closure :: bind ( $cl1 , null , 'A' );
 $bcl2 = Closure :: bind ( $cl2 , new A (), 'A' );
echo $bcl1 (), "\n" ;
echo $bcl2 (), "\n" ;
PHP 相关文章推荐
php动态生成JavaScript代码
Mar 09 PHP
ThinkPHP3.0略缩图不能保存到子目录的解决方法
Sep 30 PHP
使用php判断网页是否gzip压缩
Jun 25 PHP
PHP中is_file不能替代file_exists的理由
Mar 04 PHP
PHP使用CURL实现对带有验证码的网站进行模拟登录的方法
Jul 23 PHP
php使用cookie显示用户上次访问网站日期的方法
Jan 26 PHP
YII视图整合kindeditor扩展的方法
Jul 13 PHP
php输出图像的方法实例分析
Feb 16 PHP
弹出模态框modal的实现方法及实例
Sep 19 PHP
Smarty缓存机制实例详解【三种缓存方式】
Jul 20 PHP
PHP fopen中文文件名乱码问题解决方案
Oct 28 PHP
详解Laravel制作API接口
May 31 PHP
迪菲-赫尔曼密钥交换(Diffie?Hellman)算法原理和PHP实现版
May 12 #PHP
PHP 反射(Reflection)使用实例
May 12 #PHP
PHP Reflection API详解
May 12 #PHP
php通过curl模拟登陆DZ论坛
May 11 #PHP
PHP中的魔术方法总结和使用实例
May 11 #PHP
php基于curl扩展制作跨平台的restfule 接口
May 11 #PHP
PHP SPL标准库中的常用函数介绍
May 11 #PHP
You might like
php MsSql server时遇到的中文编码问题
2009/06/11 PHP
php获取当前网址url并替换参数或网址的方法
2010/06/06 PHP
php查询操作实现投票功能
2016/05/09 PHP
PHP5.5安装PHPRedis扩展及连接测试方法
2017/01/22 PHP
PHP获取当前URL路径的处理方法(适用于多条件筛选列表)
2017/02/10 PHP
如何运行/调试你的PHP代码
2020/10/23 PHP
基于jQuery的获得各种控件Value的方法
2010/11/19 Javascript
bootstrap和jQuery.Gantt的css冲突 如何解决
2016/05/29 Javascript
a标签置灰不可点击的实现方法
2017/02/06 Javascript
Node批量爬取头条视频并保存方法
2018/09/20 Javascript
layui按条件隐藏表格列的实例
2019/09/19 Javascript
JS数组方法shift()、unshift()用法实例分析
2020/01/18 Javascript
JavaScript实现alert弹框效果
2020/11/19 Javascript
Python Web服务器Tornado使用小结
2014/05/06 Python
Python文件及目录操作实例详解
2015/06/04 Python
深入理解Python爬虫代理池服务
2018/02/28 Python
Python针对给定字符串求解所有子序列是否为回文序列的方法
2018/04/21 Python
python3+PyQt5实现自定义分数滑块部件
2018/04/24 Python
Python 实现取矩阵的部分列,保存为一个新的矩阵方法
2018/11/14 Python
python实现大转盘抽奖效果
2019/01/22 Python
详解Python 4.0 预计推出的新功能
2019/07/26 Python
numpy中的meshgrid函数的使用
2019/07/31 Python
pytorch 常用线性函数详解
2020/01/15 Python
Python批量启动多线程代码实例
2020/02/18 Python
Pycharm2020.1安装中文语言插件的详细教程(不需要汉化)
2020/08/07 Python
Python2.6版本pip安装步骤解析
2020/08/17 Python
HTML5: Web 标准最巨大的飞跃
2008/10/17 HTML / CSS
HTML5实现自带进度条和滑块滑杆效果
2018/04/17 HTML / CSS
使用placeholder属性设置input文本框的提示信息
2020/02/19 HTML / CSS
幼儿园教师教学反思
2014/02/06 职场文书
电脑售后服务承诺书
2014/03/27 职场文书
网络工程师自荐书范文
2014/04/01 职场文书
四风问题对照检查材料整改措施
2014/09/27 职场文书
干部培训简讯
2015/07/20 职场文书
十大冰系宝可梦排名,颜值最高的阿罗拉九尾,第三使用率第一
2022/03/18 日漫
VW、VH适配移动端的解决方案与常见问题
2023/05/21 HTML / CSS