PHP SPL标准库之接口(Interface)详解
php spl标准库总共有6个接口,如下:
1.countable
2.outeriterator
3.recursiveiterator
4.seekableiterator
5.splobserver
6.splsubject
其中outeriterator、recursiveiterator、seekableiterator都是继承iterator类的,下面会对每种接口作用和使用进行详细说明。
coutable接口:
实现countable接口的对象可用于count()函数计数。
class mycount implements countable
{
public function count()
{
static $count = 0;
$count++;
return $count;
}
}
$count = new mycount();
$count->count();
$count->count();
echo count($count); //3
echo count($count); //4
说明:
调用count()函数时,mycount::count()方法被调用
count()函数的第二个参数将不会产生影响
outeriterator接口:
自定义或修改迭代过程。
//iteratoriterator是outeriterator的一个实现类
class myouteriterator extends iteratoriterator {
public function current()
{
return parent::current() . 'test';
}
}
foreach(new myouteriterator(new arrayiterator(['b','a','c'])) as $key => $value) {
echo "$key->$value".php_eol;
}
/*
结果:
0->btest
1->atest
2->ctest
*/
在实际运用中,outeriterator极其有用:
$db = new pdo('mysql:host=localhost;dbname=test', 'root', 'mckee');
$db->query('set names utf8');
$pdostatement = $db->query('select * from test1', pdo::fetch_assoc);
$iterator = new iteratoriterator($pdostatement);
$tenrecordarray = iterator_to_array($iterator);
print_r($tenrecordarray);
recursiveiterator接口:
用于循环迭代多层结构的数据,recursiveiterator另外提供了两个方法:
recursiveiterator::getchildren 获取当前元素下子迭代器
recursiveiterator::haschildren 判断当前元素下是否有迭代器
class myrecursiveiterator implements recursiveiterator
{
private $_data;
private $_position = 0;
public function __construct(array $data) {
$this->_data = $data;
}
public function valid() {
return isset($this->_data[$this->_position]);
}
public function haschildren() {
return is_array($this->_data[$this->_position]);
}
public function next() {
$this->_position++;
}
public function current() {
return $this->_data[$this->_position];
}
public function getchildren() {
print_r($this->_data[$this->_position]);
}
public function rewind() {
$this->_position = 0;
}
public function key() {
return $this->_position;
}
}
$arr = array(0, 1=> array(10, 20), 2, 3 => array(1, 2));
$mri = new myrecursiveiterator($arr);
foreach ($mri as $c => $v) {
if ($mri->haschildren()) {
echo "$c has children: " .php_eol;
$mri->getchildren();
} else {
echo "$v" .php_eol;
}
}
/*
结果:
0
1 has children:
array
(
[0] => 10
[1] => 20
)
2
3 has children:
array
(
[0] => 1
[1] => 2
)
*/
seekableiterator接口:
通过seek()方法实现可搜索的迭代器,用于搜索某个位置下的元素。
class myseekableiterator implements seekableiterator {
private $position = 0;
private $array = array(
"first element" ,
"second element" ,
"third element" ,
"fourth element"
);
public function seek ( $position ) {
if (!isset( $this -> array [ $position ])) {
throw new outofboundsexception ( "invalid seek position ( $position )" );
}
$this -> position = $position ;
}
public function rewind () {
$this -> position = 0 ;
}
public function current () {
return $this -> array [ $this -> position ];
}
public function key () {
return $this -> position ;
}
public function next () {
++ $this -> position ;
}
public function valid () {
return isset( $this -> array [ $this -> position ]);
}
}
try {
$it = new myseekableiterator ;
echo $it -> current (), "\n" ;
$it -> seek ( 2 );
echo $it -> current (), "\n" ;
$it -> seek ( 1 );
echo $it -> current (), "\n" ;
$it -> seek ( 10 );
} catch ( outofboundsexception $e ) {
echo $e -> getmessage ();
}
/*
结果:
first element
third element
second element
invalid seek position ( 10 )
*/
splobserver和splsubject接口:
splobserver和splsubject接口用来实现观察者设计模式,观察者设计模式是指当一个类的状态发生变化时,依赖它的对象都会收到通知并更新。使用场景非常广泛,比如说当一个事件发生后,需要更新多个逻辑操作,传统方式是在事件添加后编写逻辑,这种代码耦合并难以维护,观察者模式可实现低耦合的通知和更新机制。
看看splobserver和splsubject的接口结构:
//splsubject结构 被观察的对象
interface splsubject{
public function attach(splobserver $observer); //添加观察者
public function detach(splobserver $observer); //剔除观察者
public function notify(); //通知观察者
}
//splobserver结构 代表观察者
interface splobserver{
public function update(splsubject $subject); //更新操作
}
看下面一个实现观察者的例子:
class subject implements splsubject
{
private $observers = array();
public function attach(splobserver $observer)
{
$this->observers[] = $observer;
}
public function detach(splobserver $observer)
{
if($index = array_search($observer, $this->observers, true)) {
unset($this->observers[$index]);
}
}
public function notify()
{
foreach($this->observers as $observer) {
$observer->update($this);
}
}
}
class observer1 implements splobserver
{
public function update(splsubject $subject)
{
echo "逻辑1代码".php_eol;
}
}
class observer2 implements splobserver
{
public function update(splsubject $subject)
{
echo "逻辑2代码".php_eol;
}
}
$subject = new subject();
$subject->attach(new observer1());
$subject->attach(new observer2());
$subject->notify();
/*
结果:
逻辑1代码
逻辑2代码
*/
上一篇: asp下利用fso实现文件夹或文件移动改名等操作函数
下一篇: ASP 千万级数据分页的存储过程
推荐阅读