SPL是Standard PHP Library(PHP标准库)的缩写。
根据官方定义,它是"a collection of interfaces and classes that are meant to solve standard problems"。但是,目前在使用中,SPL更多地被看作是一种使object(物体)模仿array(数组)行为的interfaces和classes。
这几天学习SPL,这是我写的一点小笔记,望日后再次使用时能够想起点滴,SPL核心便是Iterator,熟练使用Iterator可以让我们更加方便的去操作数据.
ArrayIterator迭代器示例
1 <?php 2 $fruits = array 3 ( 4 "apple" => 'apple value', 5 "orange" => 'orange value', 6 "grape" => "grape value", 7 "plum" => "plum value" 8 ); 9 print_r($fruits); 10 echo "**** use fruits directly "; 11 foreach ($fruits as $key => $value) 12 { 13 echo $key . ":" . $value . " "; 14 } 15 //使用ArrayIterator遍历数组 16 $obj = new ArrayObject($fruits); 17 $it = $obj->getIterator(); 18 19 echo "***** use ArrayIterator in for "; 20 foreach ($it as $key => $value) 21 { 22 echo $key . ":" . $value . " "; 23 } 24 25 echo "***** use ArrayIterator in while "; 26 $it->rewind();//调用current之前要用rewind 27 while ($it->valid()) 28 { 29 echo $it->key() . ":" . $it->current()." "; 30 $it->next(); 31 } 32 //跳过某些元素进行打印 33 echo "***** use seek before while "; 34 $it->rewind(); 35 if ($it->valid()) 36 { 37 $it->seek(1); 38 while($it->valid()) 39 { 40 echo $it->key() . ":" . $it->current()." "; 41 $it->next(); 42 } 43 } 44 45 echo "***** use ksort "; 46 $it->ksort(); 47 foreach ($it as $key => $value) 48 { 49 echo $key . ":" . $value . " "; 50 } 51 52 echo "***** use asort "; 53 $it->asort(); 54 foreach ($it as $key => $value) 55 { 56 echo $key . ":" . $value . " "; 57 }
AppendIterator迭代器示例
1 <?php 2 $array_a = new ArrayIterator(array('a','b','c')); 3 $array_b = new ArrayIterator(array('d','e','f')); 4 $it = new AppendIterator(); 5 $it->append($array_a); 6 //通过append方法把迭代器对象添加到AppendIterator对象中 7 $it->append($array_b); 8 foreach ($it as $key => $value) 9 { 10 echo $value." "; 11 }
MultipleIterator迭代器示例
1 <?php 2 $idIter = new ArrayIterator(array('01','02','03')); 3 $nameIter = new ArrayIterator(array('张三','李四','王五')); 4 $ageIter = new ArrayIterator(array('22','23','25')); 5 $mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC); 6 $mit->attachIterator($idIter,"ID"); 7 $mit->attachIterator($nameIter,"NAME"); 8 $mit->attachIterator($ageIter,"AGE"); 9 foreach ($mit as $value) 10 { 11 print_r($value); 12 }
FilesystemIterator迭代器示例
1 <?php 2 $it = new FilesystemIterator('.'); 3 foreach ($it as $finfo) 4 { 5 printf("%s %s %8s %s ", 6 date("Y-m-d H:i:s",$finfo->getMTime()), 7 $finfo->isDir()?"<DIR>":"", 8 number_format($finfo->getSize()), 9 $finfo->getFileName() 10 ); 11 }
Countable接口示例
1 <?php 2 class CountMe implements Countable 3 { 4 protected $_myCount = 3; 5 public function count() 6 { 7 return $this->_myCount; 8 } 9 } 10 $obj = new CountMe(); 11 echo count($obj);
OuterIterator接口示例
1 <?php 2 $array = ['Value1','Value2','Value3','Value4']; 3 $outerObj = new OuterImpl(new ArrayIterator($array)); 4 5 foreach ($outerObj as $key => $value) 6 { 7 echo "++" . $key . " - " . $value . " "; 8 } 9 10 class OuterImpl extends IteratorIterator 11 { 12 public function current() 13 { 14 return parent::current()."_tail"; 15 } 16 public function key() 17 { 18 return "Pre_".parent::key(); 19 } 20 }
autoload机制的三种用法
1 <?php 2 //设置autoload寻找php定义的类文件的扩展名,多个扩展名用逗号分隔,前面的扩展名优先被匹配 3 spl_autoload_extensions('.class.php,.php'); 4 //设置autoload寻找php定义的类文件的目录,多个目录用PATH_SEPARATOR进行分隔 5 set_include_path(get_include_path().PATH_SEPARATOR."libs/"); 6 //提示PHP使用Autoload机制查找类定义 7 spl_autoload_register(); 8 new Test();
1 <?php 2 //定义__autoload函数,可以在不调用spl_autoload_register函数的情况完成类的装载 3 function __autoload($class_name) 4 { 5 echo "__autoload class:".$class_name." "; 6 require_once ($class_name.'.php'); 7 } 8 //定义一个替换__autoload函数的类文件装载函数 9 function classLoader($class_name) 10 { 11 echo "classLoader() load class:".$class_name." "; 12 require_once ($class_name.'.php'); 13 } 14 //传入定义好的装载类的函数名称替换__autoload 15 spl_autoload_register('classLoader'); 16 new Test();
1 <?php 2 3 //定义一个替换__autoload函数的类文件装载函数 4 function classLoader($class_name) 5 { 6 echo "classLoader() load class:".$class_name." "; 7 set_include_path("."); 8 spl_autoload($class_name);//当我们不用require或者requir_once载入类文件的时候,而想通过系统查找include_path来装载类是,必须显式调用spl_autoload函数,参数是类的名称来重启类文件的自动查找(装载) 9 } 10 //传入定义好的装载类的函数名称替换__autoload 11 spl_autoload_register('classLoader'); 12 new Test();