比如遍历目录这段代码,请问spl比传统代码好在哪呢?
spl实现
getinneriterator(); if ($item->isfile() && in_array(pathinfo($item->getfilename(), pathinfo_extension), $this->ext)) { return true; } }}// 实例化foreach (new recursivefilefilteriterator('d:/history') as $item) { echo $item . php_eol;}
传统方法:
回复内容: 我想问下,php5 提供的spl有什么好处,以前写个array就可以解决,为什么现在非要包装一个类呢?
比如遍历目录这段代码,请问spl比传统代码好在哪呢?
spl实现
getinneriterator(); if ($item->isfile() && in_array(pathinfo($item->getfilename(), pathinfo_extension), $this->ext)) { return true; } }}// 实例化foreach (new recursivefilefilteriterator('d:/history') as $item) { echo $item . php_eol;}
传统方法:
抽象,外部代码可以根据 filteriterator 来操作,不需要管是否是操作文件系统,比如你以后可能会将文件系统转换为数据库。
项目可能出现的演变情况:
recursivefilefilteriterator => recursivedatabasefilteriterator => recursivememoryfilteriterator
