downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

IteratorAggregate::getIterator> <Iterator::valid
[edit] Last updated: Fri, 23 Mar 2012

view this page in

IteratorAggregate Arayüzü

(PHP 5 >= 5.0.0)

Giriş

Harici bir yineleyici oluşturmak için arayüz.

Arayüz Sözdizimi

IteratorAggregate extends Traversable {
/* Yöntemler */
abstract public Traversable getIterator ( void )
}

Örnek 1 - Temel kullanımı

<?php
class Verilerim implements IteratorAggregate {
    public 
$özellik1 "Birinci genel özellik";
    public 
$özellik2 "İkinci genel özellik";
    public 
$özellik3 "Üçüncü genel özellik";

    public function 
__construct() {
        
$this->özellik4 "son özellik";
    }

/* PHP, tr_TR yerelinde bu yöntemi de arıyor, yoksa
 * "Fatal error: Couldn't find implementation for method
 * Verilerim::getiterator in Unknown on line 0"
 * hatasını veriyor.
 */
    
public function getiterator() {
        return new 
ArrayIterator($this);
    }

    public function 
getIterator() {
        return new 
ArrayIterator($this);
    }
}

$obj = new Verilerim;

foreach(
$obj as $key => $value) {
    
var_dump($key$value);
    echo 
"\n";
}
?>

Yukarıdaki örnek şuna benzer bir çıktı üretir:

string(9) "özellik1"
string(22) "Birinci genel özellik"

string(9) "özellik2"
string(22) "İkinci genel özellik"

string(9) "özellik3"
string(25) "Üçüncü genel özellik"

string(9) "özellik4"
string(12) "son özellik"

İçindekiler



add a note add a note User Contributed Notes IteratorAggregate
Tab Atkins 29-Apr-2012 02:11
Note that, at least as of 5.3, you still aren't allowed to return a normal Array from getIterator().

In some places, the docs wrap the array into an ArrayObject and return that.  DON'T DO IT.  ArrayObject drops any empty-string keys on the floor when you iterate over it (again, at least as of 5.3).

Use ArrayIterator instead.  I wouldn't be surprised if it didn't have its own set of wonderful bugs, but at the very least it works correctly when you use it with this method.

 
show source | credits | sitemap | contact | advertising | mirror sites