PHP瀵硅薄杩唬


瀵硅薄杩唬,PHP5鎻愪緵浜嗕竴绉嶈凯浠o紙iteration锛夊璞$殑鍔熻兘锛屽氨鍍忎娇鐢ㄦ暟缁勯偅鏍凤紝鍙互閫氳繃foreach 鏉ラ亶鍘嗗璞′腑鐨勫睘鎬с傞粯璁ゆ儏鍐典笅锛屽湪澶栭儴杩唬鍙兘寰楀埌澶栭儴鍙鐨勫睘鎬х殑鍊笺

瀵硅薄杩唬

PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration. PHP5鎻愪緵浜嗕竴绉嶈凯浠o紙iteration锛夊璞$殑鍔熻兘锛屽氨鍍忎娇鐢ㄦ暟缁勯偅鏍凤紝鍙互閫氳繃foreach 鏉ラ亶鍘嗗璞′腑鐨勫睘鎬с傞粯璁ゆ儏鍐典笅锛屽湪澶栭儴杩唬鍙兘寰楀埌澶栭儴鍙鐨勫睘鎬х殑鍊笺

Example #1 绠鍗曠殑瀵硅薄杩唬

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<code><?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: ";
       foreach($this as $key => $value) {
           print "$key => $value ";
       }
    }
}
 
$class = new MyClass();
 
foreach($class as $key => $value) {
    print "$key => $value ";
}
echo " ";
 
 
$class->iterateVisible();
 
?></code>

浠ヤ笂渚嬬▼浼氳緭鍑猴細

1
2
3
4
5
6
7
8
9
10
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

濡備笂鎵绀,the foreach 閬嶅巻浜嗘墍鏈 鍙鐨 灞炴. 浣犱篃鍙互閫氳繃瀹炵幇PHP 5鑷甫鐨 Iterator鎺ュ彛鏉ュ疄鐜拌凯浠. 浣跨敤Iterator鎺ュ彛鍙互璁╁璞¤嚜琛屽喅瀹氬浣曡凯浠h嚜宸层
 

Example #2 瀹炵幇Iterator鎺ュ彛鐨勮凯浠

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<code><?php
class MyIterator implements Iterator
{
    private $var = array();
 
    public function __construct($array)
    {
        if (is_array($array)) {
            $this->var = $array;
        }
    }
 
    public function rewind() {
        echo "rewinding ";
        reset($this->var);
    }
 
    public function current() {
        $var = current($this->var);
        echo "current: $var ";
        return $var;
    }
 
    public function key() {
        $var = key($this->var);
        echo "key: $var ";
        return $var;
    }
 
    public function next() {
        $var = next($this->var);
        echo "next: $var ";
        return $var;
    }
 
    public function valid() {
        $var = $this->current() !== false;
        echo "valid: {$var} ";
        return $var;
    }
}
 
$values = array(1,2,3);
$it = new MyIterator($values);
 
foreach ($it as $a => $b) {
    print "$a: $b ";
}
?></code>

浠ヤ笂渚嬬▼浼氳緭鍑猴細

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:

浣犱篃鍙互璁╃被瀹炵幇IteratorAggregate鎺ュ彛锛岃繖鏍蜂綘鐨勭被灏变笉鐢ㄥ己鍒舵у湴瀹炵幇 Iterator鎺ュ彛涓殑鎵鏈夋柟娉曘

Example #3 閫氳繃IteratorAggregate鏉ュ疄鐜板璞¤凯浠

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<code><?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] ";
}
?></code>

浠ヤ笂渚嬬▼浼氳緭鍑猴細

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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:

浠ヤ笂灏辨槸PHP瀵硅薄杩唬鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=108