Feeds:
文章
留言

Archive for 2009 年 06 月

可用來限制資料集合,也可應用成分頁資料。

$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$arrIter = new ArrayIterator($arr);
$offset = 5;
$total = 2;
$filterIter = new LimitIterator($arrIter, $offset, $total);
foreach($filterIter as $item){
    echo $item . ‘<br/>’;
}

執行結果會顯示 6 和 7

廣告

Read Full Post »

從 SplFileInfo 繼承,用於處理檔案資料。如下顯示文字檔案內容

file_put_contents(‘/tmp/test.txt’, file_get_contents(‘http://www.google.com’) );
foreach(new SplFileObject(‘/tmp/test.txt’) as $line){
    echo $line . ‘<br/>’;
}

執行結果

image

Read Full Post »

迭代資料過程中過濾 資料用。如下:

class MyFilter extends FilterIterator{
    function __construct($iter){
        parent::__construct($iter);
    }

    function accept(){
        return $this->current() % 2 == 0;
    }

}
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$arrIter = new ArrayIterator($arr);
$filterIter = new MyFilter($arrIter);

foreach($filterIter as $item){
    echo $item . ‘<br/>’;
}

執行結果

image

Read Full Post »

用來檢視指定目錄下的檔案和目錄集合。不過不支援迭代所有的子目錄,必須自己添加。

$folder = new DirectoryIterator(‘./webmeet/theme’);
// $item:SplFileInfo
foreach($folder as $item){
    if ($item->isDir()){
        echo ‘[‘ . $item->getFilename() . ‘]<br/>’;
    }else{
        echo $item->getFilename() . ‘<br/>’;
    }
}

image

Read Full Post »

用來回傳資料數量,以下範例是用 for 迭代實作 ArrayAccess + Countable 介面的物件。

class My implements ArrayAccess, Countable{
    private $name = ‘cwchiu’;
    private $sex = 1;
    private $age = 30;

    function offsetExists($offset){
        if ($offset >=0 && $offset <$this->count()){
            return true;
        }

        return false;
    }

    function offsetGet($offset){
        switch($offset){
        case 0:
            return $this->name;
        case 1:
            return $this->sex;
        case 2:
            return $this->age;
        }
    }

    function offsetSet($offset, $value){
        switch($offset){
        case 0:
            $this->name = $value;
        case 1:
            $this->age = $value;
        case 2:
            $this->age = $value;
        }
    }

    function offsetUnset($offset){
        switch($offset){
        case 0:
            unset( $this->name );
        case 1:
            unset( $this->sex );
        case 2:
            unset( $this->age );
        }
    }

    function count (){
        return 3;
    }

}

$aMy = new My();
for($i=0, $c=$aMy->count(); $i<$c; ++$i){
    echo $aMy[$i] . ‘<br/>’;
}

Read Full Post »

繼承 Iterator 介面,並新增 Seek( ) 用於快速移到指定位置。下面是一個簡單的範例

class My implements SeekableIterator{
    private $i=0;
    function rewind(){
        $this->i = 0;
    }

    function current(){
        return $this->i *5;
    }

    function key(){
        return $this->i;
    }

    function next(){
        ++$this->i;
    }

    function valid(){
        return $this->i<=5;
    }

    function seek($offset){
        if ($offset >5 || $offset < 0 ){
            throw new OutOfBoundsException(‘invalid pos’);
        }

        $this->i = $offset;
    }
}

$aMy = new My();
echo $aMy->current() . ‘<br/>’;
$aMy->seek(5);
echo $aMy->current() . ‘<br/>’;

執行結果:

image

Read Full Post »

讓物件支援序列化。如下範例

<?php

class My implements Serializable {
private $data;
function __construct($data){
$this->data = $data;
}

function serialize(){
return implode(‘,’, $this->data);
}

function unserialize($s){
$this->data = explode(‘,’, $s);
}
}

$aMy = new My(array(1, 2, 3, 4, 5));
echo ‘[init State]<br/>’;
print_r($aMy);
echo ‘<hr/> [Serialize]<br/>’;
$data = serialize($aMy);
echo ‘Serialize: >>’ . $data;
echo ‘<hr/> [Unserialize]<br/>’;
$aMy2 = unserialize($data);
print_r($aMy2);

?>

執行結果

image

Read Full Post »

« Newer Posts - Older Posts »