duanshan3331 2013-11-17 10:52
浏览 33
已采纳

当我们需要SPL IteratorAggregate?

I read the example of IteratorAggregate in PHP manual. I try to remove the IteratorAggregate interface but the outputs are similar with the implemented one.

<?php    
    class myData {
        public $property1 = "Public property one";
        public $property2 = "Public property two";
        public $property3 = "Public property three";

        public function __construct() {
            $this->property4 = "last property";
        }
    }

    $obj = new myData;

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

Outputs:

string(9) "property1"
string(19) "Public property one"

string(9) "property2"
string(19) "Public property two"

string(9) "property3"
string(21) "Public property three"

string(9) "property4"
string(13) "last property"

I want to know that when we should implement IteratorAggregate interface? And why the ouputs are identical?

  • 写回答

2条回答 默认 最新

  • dongqiuwei8667 2013-11-17 11:01
    关注

    The example in the manual page is not a great one, precisely because of what you describe here. The example class implements IteratorAggregate and does this:

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

    ArrayIterator, when provided with an object, simply provides the public properties of the object. This can be done, as you demonstrate, simply by doing foreach on the original object.

    A more useful example would be if we wanted to reflect a different selection of values. For instance, let's say all those elements were private and we didn't want to send $property3:

    class myData implements IteratorAggregate {
        private $property1 = "Public property one";
        private $property2 = "Public property two";
        private $property3 = "Public property three";
    
        public function __construct() {
            $this->property4 = "last property";
        }
    
        public function getIterator() {
            return new ArrayIterator(array(
                'property1' => $this->property1,
                'property2' => $this->property2,
                'property4' => $this->property4
            ));
        }
    }
    
    $obj = new myData;
    
    foreach($obj as $key => $value) {
        var_dump($key, $value);
        echo "
    ";
    }
    

    This would construct an iterator with the properties you wanted, not all of them.

    You could construct any iterator in this way, even one entirely unrelated to the properties of the class. Often the best way to do this would be by implementing Iterator, but sometimes if will be quicker to construct a new iterator object and point to that with getIterator.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么