php traverse objects

伊谢尔伦
Release: 2016-11-23 14:01:15
Original
1741 people have browsed it

PHP 5 provides a way to define objects so that they can be traversed through a list of cells, such as using the foreach statement. By default, all visible properties will be used for traversal.

Example #1 Simple object traversal

class MyClass
{
    public $var1 = 1;
    public $var2 = 2;
    public $var3 = 3;
    protected $protected = 'protected var';
    private $private = 'private var';
    function iterateVisible(){
        echo "MyClass::iterateVisible:<br>";
        foreach($this as $key => $value){
            print "$key=>$value<br>";
        }
    }
} 
$class = new MyClass();
foreach($class as $key => $value){
    print "$key => $value<br>";
}
echo "<br>";
$class->iterateVisible();
Copy after login

Output result:

var1 => 1

var2 => 2

var3 => 3

MyClass::iterateVisible:

var1=>1

var2=>2

var3=>3

protected=>protected var

private=>private var

As shown above, foreach traverses all the visible properties it can access.

Going a step further, you can implement the Iterator interface. You can let the object decide for itself how to traverse and which values ​​are available each time it is traversed.

class MyIterator implements Iterator{
    private $var = array();
    public function __construct($array)
    {
        if(is_array($array)){
            $this->var = $array;
        }
    }
    public function rewind(){
        echo "rewinding<br>";
        reset($this->var);
    }
    public function current(){
        $var = current($this->var);
        echo "current:$var<br>";
        return $var;
    }
    public function key(){
        $var = key($this->var);
        echo "key:$var<br>";
        return $var;
    }
    public function next(){
        $var = next($this->var);
        echo "next:$var<br>";
        return $var;
    }
    public function valid(){
        $var = $this->current()!==false;
        echo "valid:$var<br>";
        return $var;
    }
}
$values = array(1,2,3);
$it = new MyIterator($values);
foreach($it as $a => $b){
    print "$a:$b<br>";
}
Copy after login

Output result:

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:

You can use the IteratorAggregate interface instead of implementing all Iterator methods. IteratorAggregate only needs to implement one method, IteratorAggregate::getIterator(), which should return an instance of the class that implements Iterator.

Example #3 Traverse objects by implementing IteratorAggregate

include_once(&#39;class2.php&#39;);
class MyCollection implements IteratorAggregate
{
    private $items = array();
    private $count = 0;
    public function getIterator(){
       return new MyIterator($this->items);
    }
    public function add($value){
        $this->items[$this->count++] = $value;
    }
}
$coll = new MyCollection();
$coll -> add(&#39;1&#39;);
$coll -> add(&#39;2&#39;);
$coll -> add(&#39;3&#39;);
foreach($coll as $k => $v){
    echo "key/value:[$k->$v]<br><br>";
}
Copy after login

Output result:

rewinding

current:1

valid:1

current:1

key:0

key/value:[0-> ;1]

next:2

current:2

valid:1

current:2

key:1

key/value:[1->2]

next:3

current:3

valid:1

current:3

key:2

key/value:[2->3]


next:

current:

valid:


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