Spl:handle data like object

  1. 所有spl
  2. same result different progressing
  3. 目标
  4. by the way
  5. 附.spl函数
  6. 参考

数据可以方便的通过循环遍历来方便的获取到每一条详细的数据,数组也成了php非常好的数据容器,数组为什么能实现这样的功能?循环和遍历,是因为他集成了来自ArrayIterator对象,这个对象使他拥有了访问每个元素的本领。

所有spl

先来看一个方法:spl_classes,输出所有spl 类名:

[AppendIterator] => AppendIterator
[ArrayIterator] => ArrayIterator
[ArrayObject] => ArrayObject
[BadFunctionCallException] => BadFunctionCallException
[BadMethodCallException] => BadMethodCallException
[CachingIterator] => CachingIterator
[CallbackFilterIterator] => CallbackFilterIterator
[Countable] => Countable
[DirectoryIterator] => DirectoryIterator
[DomainException] => DomainException
[EmptyIterator] => EmptyIterator
[FilesystemIterator] => FilesystemIterator
[FilterIterator] => FilterIterator
[GlobIterator] => GlobIterator
[InfiniteIterator] => InfiniteIterator
[InvalidArgumentException] => InvalidArgumentException
[IteratorIterator] => IteratorIterator
[LengthException] => LengthException
[LimitIterator] => LimitIterator
[LogicException] => LogicException
[MultipleIterator] => MultipleIterator
[NoRewindIterator] => NoRewindIterator
[OuterIterator] => OuterIterator
[OutOfBoundsException] => OutOfBoundsException
[OutOfRangeException] => OutOfRangeException
[OverflowException] => OverflowException
[ParentIterator] => ParentIterator
[RangeException] => RangeException
[RecursiveArrayIterator] => RecursiveArrayIterator
[RecursiveCachingIterator] => RecursiveCachingIterator
[RecursiveCallbackFilterIterator] => RecursiveCallbackFilterIterator
[RecursiveDirectoryIterator] => RecursiveDirectoryIterator
[RecursiveFilterIterator] => RecursiveFilterIterator
[RecursiveIterator] => RecursiveIterator
[RecursiveIteratorIterator] => RecursiveIteratorIterator
[RecursiveRegexIterator] => RecursiveRegexIterator
[RecursiveTreeIterator] => RecursiveTreeIterator
[RegexIterator] => RegexIterator
[RuntimeException] => RuntimeException
[SeekableIterator] => SeekableIterator
[SplDoublyLinkedList] => SplDoublyLinkedList
[SplFileInfo] => SplFileInfo
[SplFileObject] => SplFileObject
[SplFixedArray] => SplFixedArray
[SplHeap] => SplHeap
[SplMinHeap] => SplMinHeap
[SplMaxHeap] => SplMaxHeap
[SplObjectStorage] => SplObjectStorage
[SplObserver] => SplObserver
[SplPriorityQueue] => SplPriorityQueue
[SplQueue] => SplQueue
[SplStack] => SplStack
[SplSubject] => SplSubject
[SplTempFileObject] => SplTempFileObject
[UnderflowException] => UnderflowException
[UnexpectedValueException] => UnexpectedValueException

same result different progressing

异曲同工

假设这样的情况,我们在一个数组中保存了需要处理的数据,接下来我们需要在这个数组的结尾或者开头补充些数据,已让他更加丰富,如果是一般操作,没问题 很简单:

$data = array('alibaba.com', 'baidu.com');

// 向数组头部增加一个元素
array_unshift($data, 'whoisthenext.com');
// 向数据尾部增加一个元素
array_push($data, 'tencent.com');

另外一种解决方案:

$data = array('alibaba.com', 'baidu.com');
$dataObject = new ArrayObject($data);

// 数组附加元素
$dataObject->append('last.com');

而且,促使我们使用各种迭代器的另外一个原因是任何一个programer都不能抗拒的:速度。
在处理如下的数据时,通过ab测试结果表明,使用迭代器之后请求处理速度明显快于没使用迭代器的情况。

目标

将下面的xml数据做成一个可以循环的对象。

$xmlstring = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<input_charset>UTF-8</input_charset>
<out_refund_no />
<out_trade_no />
<partner>1226251301</partner>
<reccv_user_name />
<recv_user_id />
<refund_channel />
<refund_fee />
<refund_id />
<refund_status>-1</refund_status>
<retcode>88222013</retcode>
<retmsg>[20921191]退款总金额超出交易金额[20150313174732-183]</retmsg>
<sign>010651C7B3906F2863B3B1D2FB63A571</sign>
<sign_key_index>1</sign_key_index>
<sign_type>MD5</sign_type>
<transaction_id />
</root>
XML;

First method:

<?php
try {
/*** a new simpleXML iterator object ***/
$sxi =new SimpleXMLIterator($xmlstring);

foreach ( $sxi as $node )
{
foreach($node as $k=>$v)
{
echo $v->species.'<br />';
}
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>

Second method:

try {
$sxe = simplexml_load_string($xmlstring, 'SimpleXMLIterator');

for ($sxe->rewind(); $sxe->valid(); $sxe->next())
{
if($sxe->hasChildren())
{
foreach($sxe->getChildren() as $element=>$value)
{
echo $value->species.'<br />';
}
}
}
}
catch(Exception $e)
{
echo $e->getMessage();
}

第一种方法的比第二种方法有明显的速度优势。尽管simplexml_load_stringSimpleXMLIterator是来自同一个扩展simplexml.

by the way

看到官方的Iterator大部分的中文手册还未翻译,顺手先将arrayObject部分先提交了下翻译,都是些简单易懂的句子或词,剩下的看审核通过了再说,也算是对社区的一点贡献了。

附.spl函数

class_​implements 返回指定的类实现的所有接口
class_​parents 返回指定类的父类
class_​uses Return the traits used by the given class
iterator_​apply 为迭代器中每个元素调用一个用户自定义函数
iterator_​count 计算迭代器中元素的个数
iterator_​to_​array 将迭代器中的元素拷贝到数组
spl_​autoload_​call 尝试调用所有已注册的__autoload()函数来装载请求类
spl_​autoload_​extensions 注册并返回spl_autoload函数使用的默认文件扩展名
spl_​autoload_​functions 返回所有已注册的__autoload()函数
spl_​autoload_​register 注册给定的函数作为 __autoload 的实现
spl_​autoload_​unregister 注销已注册的__autoload()函数
spl_​autoload  __autoload()函数的默认实现
spl_​classes 返回所有可用的SPL类
spl_​object_​hash 返回指定对象的hash id

参考

http://www.4wei.cn/archives/1002433
http://www.phpro.org/tutorials/Introduction-to-SPL.html

script>