Home > Backend Development > PHP Tutorial > PHP SPL核心库相对以前有什么好处嘛?

PHP SPL核心库相对以前有什么好处嘛?

WBOY
Release: 2016-06-06 20:50:50
Original
1074 people have browsed it

我想问下,PHP5 提供的SPL有什么好处,以前写个array就可以解决,为什么现在非要包装一个类呢?

比如遍历目录这段代码,请问SPL比传统代码好在哪呢?

SPL实现

<?php class RecursiveFileFilterIterator extends FilterIterator {
    // 满足条件的扩展名
    protected $ext = array('jpg','gif');

    /**
     * 提供 $path 并生成对应的目录迭代器
     */
    public function __construct($path) {
        parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
    }

    /**
     * 检查文件扩展名是否满足条件
     */
    public function accept() {
        $item = $this->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;
}
Copy after login
Copy after login

传统方法:

<?php //http://blog.ddian.cn/?post=839
    function dirSize($directoty){
    	$dir_size=0;

    	if($dir_handle=@opendir($directoty))
    	{
    		while($filename=readdir($dir_handle)){
    			$subFile=$directoty.DIRECTORY_SEPARATOR.$filename;
    			if($filename=='.'||$filename=='..'){
    				continue;
    			}elseif (is_dir($subFile))
    			{
    				$dir_size+=dirSize($subFile);
    			}elseif (is_file($subFile)){
    				$dir_size+=filesize($subFile);
    			}
    		}
    		closedir($dir_handle);
    	}
    	return ($dir_size);
    }

    $dir_size=dirSize("xym");
    echo round($dir_size/pow(1024,1),2)."KB";
?>
Copy after login
Copy after login

回复内容:

我想问下,PHP5 提供的SPL有什么好处,以前写个array就可以解决,为什么现在非要包装一个类呢?

比如遍历目录这段代码,请问SPL比传统代码好在哪呢?

SPL实现

<?php class RecursiveFileFilterIterator extends FilterIterator {
    // 满足条件的扩展名
    protected $ext = array('jpg','gif');

    /**
     * 提供 $path 并生成对应的目录迭代器
     */
    public function __construct($path) {
        parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
    }

    /**
     * 检查文件扩展名是否满足条件
     */
    public function accept() {
        $item = $this->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;
}
Copy after login
Copy after login

传统方法:

<?php //http://blog.ddian.cn/?post=839
    function dirSize($directoty){
    	$dir_size=0;

    	if($dir_handle=@opendir($directoty))
    	{
    		while($filename=readdir($dir_handle)){
    			$subFile=$directoty.DIRECTORY_SEPARATOR.$filename;
    			if($filename=='.'||$filename=='..'){
    				continue;
    			}elseif (is_dir($subFile))
    			{
    				$dir_size+=dirSize($subFile);
    			}elseif (is_file($subFile)){
    				$dir_size+=filesize($subFile);
    			}
    		}
    		closedir($dir_handle);
    	}
    	return ($dir_size);
    }

    $dir_size=dirSize("xym");
    echo round($dir_size/pow(1024,1),2)."KB";
?>
Copy after login
Copy after login

抽象,外部代码可以根据 FilterIterator 来操作,不需要管是否是操作文件系统,比如你以后可能会将文件系统转换为数据库。

项目可能出现的演变情况:

<code>RecursiveFileFilterIterator => RecursiveDatabaseFilterIterator => RecursiveMemoryFilterIterator
</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template