版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/mayongzhan - 马永占,myz,mayongzhan
StandardPHPlibrary Module (SPL标准库)
关于SPL的产生可以参考php源码包内的ext下的spl
SPL分为几大类
1. Iterators : SPL offers some advanced iterator algorithms。迭代器: SPL 提供了一些高级的迭代器运算法则。
2. Directories and Files : SPL offers two advanced directory and file handling classes:目录和文件:SPL提供了两个高级路径和文件处理类。
3. XML : SPL offers an advanced XML handling class:XML : SPL提供了一个高级XML处理类。
4. Array Overloading : SPL offers advanced Array overloading:数组重载 :SPL提供了高级数组重载。
5. Counting : interface Countable allows to hook into the standard array function count().计数:接口Countable 允许勾住标准数组方法count().
6. Exceptions : SPL provides a set of standard Exception classes each meant to indicate a certain problem type. 异常:SPL规定了一套标准异常类,每个类都标识了一类确定的问题。
7. Observer: SPL suggests a standard way of implementing the observer pattern.观察者:SPL提出了实现观察者模式的标准。
简单的来说这些都是object,都是php已经封装好的为了方便某些功能的类,相当符合面向对象...
一些例子:
得到SPL相关类的方法
DirectorIterator
ArrayObject
ArrayIterator
Recursive Array Iterator
PDO & IteratorIterator
FilterIterator
XML & RecursiveIteratorIterator
SimpleXMLIterator
LimitIterator & ArrayIterator
SPLFileObject
得到SPL相关类的方法
<?php
foreach(get_class_methods(new DirectoryIterator('./')) as $key=>$method)
{
echo $key.' -> '.$method.'<br />';
}
?>
DirectoryIterator
<?php
try{
/*** class create new DirectoryIterator Object ***/
foreach ( new DirectoryIterator('./') as $Item )
{
echo $Item.'<br />';
}
}
/*** if an exception is thrown, catch it here ***/
catch(Exception $e){
echo 'No files Found!<br />';
}
?>
ArrayObject
<?php
/*** a simple array ***/
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');
/*** create the array object ***/
$arrayObj = new ArrayObject($array);
/*** iterate over the array ***/
for($iterator = $arrayObj->getIterator();
/*** check if valid ***/
$iterator->valid();
/*** move to the next array member ***/
$iterator->next())
{
/*** output the key and current array value ***/
echo $iterator->key() . ' => ' . $iterator->current() . '<br />';
}
?>
ArrayIterator
<?php
/*** a simple array ***/
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');
try {
/*** create a new object ***/
$object = new ArrayIterator($array);
/*** rewind to the beginning of the array ***/
$object->rewind();
/*** check for valid member ***/
while($object->valid())
{
/*** echo the key and current value ***/
echo $object->key().' -> '.$object->current().'<br />';
/*** hop to the next array member ***/
$object->next();
}
}
catch (Exception $e)
{
echo $e->getMessage();
}
?>
<?php
$array = array(
array('name'=>'butch', 'sex'=>'m', 'breed'=>'boxer'),
array('name'=>'fido', 'sex'=>'m', 'breed'=>'doberman'),
array('name'=>'girly','sex'=>'f', 'breed'=>'poodle')
);
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key=>$value)
{
echo $key.' -- '.$value.'<br />';
}
?>
PDO & IteratorIterator
<?php
// check for errors
error_reporting(E_ALL);
try {
$dsn = new PDO("sqlite2:/home/kevin/html/periodic.sdb");
// the result only implements Traversable
$stmt = $dsn->prepare('SELECT * FROM periodic ORDER BY atomicnumber');
// exceute the query
$stmt->execute();
// the result should be an instance of PDOStatement
// IteratorIterator converts it and after that you can do any iterator operation with it
// The iterator will fetch the results for us.
$it = new IteratorIterator($stmt);
// the iterator object now has 5 arrays within.
// Each array contains a result set from the db query
foreach($it as $row)
{
// create the array object
$arrayObj = new ArrayObject($row);
// iterate over the array
for($iterator = $arrayObj->getIterator();
// check if valid
$iterator->valid();
// move to the next array member
$iterator->next())
{
// output the key and current array value
echo $iterator->current() . '<br />';
}
echo '<hr />';
}
$dsn = null;
}
catch (PDOException $e)
{
print "Error!: " . $e->getMessage() . "<br />";
}
?>
FilterIterator
<?php
/*** a simple array ***/
$animals = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'NZ'=>'kiwi', 'kookaburra', 'platypus');
class CullingIterator extends FilterIterator{
/*** The filteriterator takes a iterator as param: ***/
public function __construct( Iterator $it ){
parent::__construct( $it );
}
/*** check if key is numeric ***/
function accept(){
return is_numeric($this->key());
}
}/*** end of class ***/
$cull = new CullingIterator(new ArrayIterator($animals));
foreach($cull as $key=>$value)
{
echo $key.' == '.$value.'<br />';
}
?>
XML & RecursiveIteratorIterator
<?php
/*** a simple xml tree ***/
$xmlstring = <<<XML
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<document>
<animal>
<category id="26">
<species>Phascolarctidae</species>
<type>koala</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="27">
<species>macropod</species>
<type>kangaroo</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="28">
<species>diprotodon</species>
<type>wombat</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="31">
<species>macropod</species>
<type>wallaby</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="21">
<species>dromaius</species>
<type>emu</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="22">
<species>Apteryx</species>
<type>kiwi</type>
<name>Troy</name>
</category>
</animal>
<animal>
<category id="23">
<species>kingfisher</species>
<type>kookaburra</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="48">
<species>monotremes</species>
<type>platypus</type>
<name>Bruce</name>
</category>
</animal>
<animal>
<category id="4">
<species>arachnid</species>
<type>funnel web</type>
<name>Bruce</name>
<legs>8</legs>
</category>
</animal>
</document>
XML;
/*** a new simpleXML iterator object ***/
try {
/*** a new simple xml iterator ***/
$it = simplexml_load_string($xmlstring, 'SimpleXMLIterator');
/*** a new limitIterator object ***/
foreach(new RecursiveIteratorIterator($it, 1) as $name => $data)
{
echo $name.' -- '.$data.'<br />';
}
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
SimpleXMLIterator
<?php
$xmlstring =<<<XML
<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<document>
<animal>koala</animal>
<animal>kangaroo</animal>
<animal>wombat</animal>
<animal>wallaby</animal>
<animal>emu</animal>
<animal>kiwi</animal>
<animal>kookaburra</animal>
<animal>platypus</animal>
<animal>funnel web</animal>
</document>
XML;
try {
/*** a new simpleXML iterator object ***/
$sxi = new SimpleXMLIterator($xmlstring);
/*** add an attribute with a namespace ***/
$sxi->addAttribute('id:att1', 'good things', 'urn::test-foo');
/*** add an attribute without a namespace ***/
$sxi->addAttribute('att2', 'no-ns');
echo htmlentities($sxi->saveXML());
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
LimitIterator & ArrayIterator
<?php
/*** the offset value ***/
$offset = 3;
/*** the limit of records to show ***/
$limit = 2;
$array = array('koala', 'kangaroo', 'wombat', 'wallaby', 'emu', 'kiwi', 'kookaburra', 'platypus');
$it = new LimitIterator(new ArrayIterator($array), $offset, $limit);
foreach($it as $k=>$v)
{
echo $it->getPosition().'<br />';
}
?>
SPLFileObject
<?php
try{
// create a new spl file object from a file
$file = new SplFileObject("./test.log");
// check if for validity
while($file->valid())
{
// echo the current line
echo $file->current().'<br />';
// increment the iterator
$file->next();
}
}
catch (Exception $e)
{
echo $e->getMessage();
}
?>