PHP 오브젝트 이터레이션

PHP/매뉴얼 번역 2008/06/20 00:39
Object Iteration(Object Iteration)

PHP 5 는 아이템리스트에 대해 반복처리 (예를 들면 foreach 명령)를 가능하도록 오브젝트를 정의하는 수단을 제공한다. 디폴트로 모든 억세스권한이 있는 프로퍼티는 반복처리에 사용할 수 있다.

<?php
class MyClass
{
    public
$var1 = 'value 1';
    public
$var2 = 'value 2';
    public
$var3 = 'value 3';

    protected
$protected = 'protected var';
    private  
$private   = 'private var';

    function
iterateVisible() {
       echo
"MyClass::iterateVisible:\n";
       foreach(
$this as $key => $value) {
           print
"$key => $value\n";
       }
    }
}

$class = new MyClass();

foreach(
$class as $key => $value) {
    print
"$key => $value\n";
}
echo
"\n";


$class->iterateVisible();

?>
출력결과

var1 => value 1
var2 => value 2
var3 => value 3

MyClass::iterateVisible:
var1 => value 1
var2 => value 2
var3 => value 3
protected => protected var
private => private var

출력결과로 알 수 있는것처럼 foreach 에 의한 반복처리가 모든 억세스권한이 있는 변수에 대해 일어나고 있다. 또한 Iterator 라는 이름의 PHP 5 내부 interface 를 implement 할 수도 있다. 오브젝트를 어떤식으로 반복처리할까를 정할 수 있다.

Iterator를 구현한 오브젝트의 반복처리
<?php
class MyIterator implements
Iterator
{
    private
$var
= array();

    public function
__construct($array
)
    {
        if (
is_array($array
)) {
           
$this->var = $array
;
        }
    }

    public function
rewind
() {
        echo
"rewinding\n"
;
       
reset($this->var
);
    }

    public function
current
() {
       
$var = current($this->var
);
        echo
"current: $var\n"
;
        return
$var
;
    }

    public function
key
() {
       
$var = key($this->var
);
        echo
"key: $var\n"
;
        return
$var
;
    }

    public function
next
() {
       
$var = next($this->var
);
        echo
"next: $var\n"
;
        return
$var
;
    }

    public function
valid
() {
       
$var = $this->current() !== false
;
        echo
"valid: {$var}\n"
;
        return
$var
;
    }
}

$values = array(1,2,3
);
$it = new MyIterator($values
);

foreach (
$it as $a => $b
) {
    print
"$a: $b\n"
;
}
?>

결과
rewinding
current: 1
valid: 1
current: 1
key: 0
0: 1
next: 2
current: 2
valid: 1
current: 2
key: 1
1: 2
next: 3
current: 3
valid: 1
current: 3
key: 2
2: 3
next:
current:
valid:

단순히 PHP 5 의 IteratorAggregate인터페이스를 구현하여 Iterator 함수를 정의할 필요없이 자신의 클래스를 정의 할 수도 있다.

<?php
class MyCollection implements
IteratorAggregate
{
    private
$items
= array();
    private
$count = 0
;

   
// Required definition of interface IteratorAggregate
   
public function getIterator
() {
        return new
MyIterator($this->items
);
    }

    public function
add($value
) {
       
$this->items[$this->count++] = $value
;
    }
}

$coll = new MyCollection
();
$coll->add('value 1'
);
$coll->add('value 2'
);
$coll->add('value 3'
);

foreach (
$coll as $key => $val
) {
    echo
"key/value: [$key -> $val]\n\n"
;
}
?>

출력결과

rewinding
current: value 1
valid: 1
current: value 1
key: 0
key/value: [0 -> value 1]

next: value 2
current: value 2
valid: 1
current: value 2
key: 1
key/value: [1 -> value 2]

next: value 3
current: value 3
valid: 1
current: value 3
key: 2
key/value: [2 -> value 3]

next:
current:
valid:


원문링크 : http://www.php.net/manual/en/language.oop5.iterations.php

목차에
PHP 클래스와 오브젝트 ( Classes and Objects ) PHP 5

'PHP > 매뉴얼 번역' 카테고리의 다른 글

PHP 오브젝트의 비교  (0) 2008/06/20
PHP 오브젝트의 클론 생성  (0) 2008/06/20
PHP final 키워드  (0) 2008/06/20
PHP 매직메소스  (0) 2008/06/20
PHP 패턴  (0) 2008/06/20
PHP 오브젝트 이터레이션  (0) 2008/06/20
PHP 오버로드  (0) 2008/06/20
PHP 오브젝트 인터페이스  (0) 2008/06/20
PHP 클래스의 추상화  (0) 2008/06/19
PHP 클래스 정수  (0) 2008/06/19
PHP static 키워드  (0) 2008/06/19
tags : ,
Trackback 0 : Comment 0