Home  >  Article  >  Backend Development  >  Detailed explanation of how PHP traverses objects

Detailed explanation of how PHP traverses objects

藏色散人
藏色散人forward
2021-02-07 11:54:393630browse

For PHP, foreach is a very convenient and easy-to-use syntax. For almost every PHPer, it is one of the most common requests in daily life. So can objects be traversed through foreach?

The answer is yes, but there is a condition, that is, traversing an object can only obtain its public properties.

// 普通遍历
class A
{
    public $a1 = '1';
    public $a2 = '2';
    public $a3 = '3';

    private $a4 = '4';
    protected $a5 = '5';

    public $a6 = '6';

    public function test()
    {
        echo 'test';
    }
}
$a = new A();
foreach ($a as $k => $v) {
    echo $k, '===', $v, PHP_EOL;
}

// a1===1
// a2===2
// a3===3
// a6===6

No matter whether it is a method or a protected or private variable, it cannot be traversed. Only public attributes can be traversed. In fact, the Iterator pattern we talked about before when talking about design patterns is specifically used for object traversal, and PHP has prepared the relevant interfaces for us. We only need to implement this interface. The creation of the iterator pattern can be completed. For specific content, please refer to the previous series of articles on design patterns: PHP Design Patterns Iterator Pattern (recommended: "PHP Video Tutorial")

// 实现迭代器接口
class B implements Iterator
{
    private $var = [];

    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;
    }
}

$b = new B([1, 2, 3, 4]);

foreach ($b as $k => $v) {
    echo $k, '===', $v, PHP_EOL;
}

// 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: 4
// current: 4
// valid: 1
// current: 4
// key: 3
// 3===4
// next:
// current:
// valid:

If today's article only talks about what I have said before The iterator pattern is too boring, so we have to learn a more interesting application. That is to allow objects to be manipulated like arrays. This is actually using an interface that PHP has already prepared for us: ArrayAccess.

// 让类可以像数组一样操作
class C implements ArrayAccess, IteratorAggregate
{
    private $container = [];
    public function __construct()
    {
        $this->container = [
            "one" => 1,
            "two" => 2,
            "three" => 3,
        ];
    }
    public function offsetSet($offset, $value)
    {
        if (is_null($offset)) {
            $this->container[] = $value;
        } else {
            $this->container[$offset] = $value;
        }
    }
    public function offsetExists($offset)
    {
        return isset($this->container[$offset]);
    }
    public function offsetUnset($offset)
    {
        unset($this->container[$offset]);
    }
    public function offsetGet($offset)
    {
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
    }

    public function getIterator() {
        return new B($this->container);
    }
}

$c = new C();
var_dump($c);

$c['four'] = 4;
var_dump($c);

$c[] = 5;
$c[] = 6;
var_dump($c);

foreach($c as $k=>$v){
    echo $k, '===', $v, PHP_EOL;
}

// rewinding
// current: 1
// valid: 1
// current: 1
// key: one
// one===1
// next: 2
// current: 2
// valid: 1
// current: 2
// key: two
// two===2
// next: 3
// current: 3
// valid: 1
// current: 3
// key: three
// three===3
// next: 4
// current: 4
// valid: 1
// current: 4
// key: four
// four===4
// next: 5
// current: 5
// valid: 1
// current: 5
// key: 0
// 0===5
// next: 6
// current: 6
// valid: 1
// current: 6
// key: 1
// 1===6
// next: 
// current: 
// valid:

This interface requires us to implement four methods:

  • offsetSet($offset, $value), set the value according to the offset
  • offsetExists($offset ), determine whether the content exists based on the offset
  • offsetUnset($offset), delete the content based on the offset
  • offsetGet($offset), get the content based on the offset

The offset here is what we often call the subscript. By implementing these four methods, we can operate objects like arrays. Of course, in daily development, we may not use the ability to traverse these objects, including iterators, very often. Usually we will directly convert the object into an array (array) obj for the next step. However, in Java, especially in JavaBean, there is often a List8742468051c85b06f0a0af9e3e506b5c inside the class as its own object to represent its own collection state. Through comparison, we found that PHP can also achieve such capabilities, and it is more convenient to use iterators and the ArrayAccess interface to achieve similar capabilities. This is a very useful knowledge expansion. Maybe you can use these abilities in your next project!

测试代码:
https://github.com/zhangyue0503/dev-blog/blob/master/php/201912/source/PHP%E6%80%8E%E4%B9%88%E9%81%8D%E5%8E%86%E5%AF%B9%E8%B1%A1%EF%BC%9F.ph

The above is the detailed content of Detailed explanation of how PHP traverses objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete