对待实现迭代器作为数组的PHP类 [英] Treat a PHP class that implements Iterator as an array

查看:118
本文介绍了对待实现迭代器作为数组的PHP类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个实现了的Iterator 接口的类,我可以手动控制如何迭代在的foreach 循环。但是否有其他方式,我可以让我的对象表现得像一个数组?

例如,假设我有一个类在线留言它实现了的Iterator ,这样我可以遍历的foreach(新留言()为$输入)。但如果我想,说,颠倒顺序?

的foreach(array_reverse(新留言())为$输入)绝对不会起作用,因为 array_reverse 只接受的阵列

我猜我问的是,我可以使用的Iterator 的不仅仅是更多的的foreach 循环<? / p>

感谢。


解决方案

Iterator接口的目的是允许在foreach循环中使用你的对象,它的目的不是让你的对象的行为像一个数组。如果你想要的东西,就像一个数组,使用数组。

您可以随时把你的对象到一个数组使用 iterator_to_array功能,但不能逆转这一进程。

如果你看到扭转元素的顺序在迭代的对象,那么你可以创建一个可能的话,使用array_reverse()内部反向()方法的需要。事情是这样的: -

  Test类实现迭代器
{
    私人$测试= [0,1,2,3,4,5,6,7,8,9,10];
    私人$指数= 0;    公共职能电流()
    {
        返回$这个 - &GT;测试[$这个 - &GT;指数]。
    }    下一个公开函数()
    {
        $这个 - &GT;指数++;
    }    公共功能键()
    {
        返回$这个 - &GT;指数;
    }    公共功能有效()
    {
        返回使用isset($这个 - &GT;测试[$这个 - &GT;键()]);
    }    公共职能退()
    {
        $这个 - &GT;指数= 0;
    }    公共职能反向()
    {
        $这个 - &GT;测试= array_reverse($这个 - &GT;测试);
        $这个 - &GT;倒带();
    }
}$测试=新测试();
后续代码var_dump(iterator_to_array($测试));
$测试 - &GT;反向();
后续代码var_dump(iterator_to_array($测试));

输出: -

 阵列(大小= 11)
  0 =&GT; INT 0
  1 =&GT; INT 1
  2 =&GT; INT 2
  3 =&GT; INT 3
  4 =&GT; INT 4
  5 =&GT; INT 5
  6 =&GT; INT 6
  7 =&GT; INT 7
  8 =&GT; INT 8
  9 =&GT; INT 9
  10 =&GT;诠释10阵列(大小= 11)
  0 =&GT;诠释10
  1 =&GT; INT 9
  2 =&GT; INT 8
  3 =&GT; INT 7
  4 =&GT; INT 6
  5 =&GT; INT 5
  6 =&GT; INT 4
  7 =&GT; INT 3
  8 =&GT; INT 2
  9 =&GT; INT 1
  10 =&GT; INT 0

我写了code证明自己,它将发布之前的工作,我想我还不如把它扔进了答案。

If I have a class that implements the Iterator interface, I can manually control how iteration in a foreach loop. But are there other ways in which I could make my object behave like an array?

For instance, let's say I have a class Guestbook which implements Iterator, so that I can iterate foreach (new Guestbook() as $entry). But what if I want to, say, reverse the order?

foreach (array_reverse(new Guestbook()) as $entry) definitely won't work, because array_reverse will only accept an array.

I guess what I'm asking is, can I use Iterator for more than just foreach loops?

Thanks.

解决方案

The purpose of the Iterator interface is to allow your object to be used in a foreach loop, it is not intended to make your object act like an array. If you want something that acts like an array, use an array.

You can always turn your object into an array by using the iterator_to_array function, but you can't reverse that process.

If you see the need for reversing the order of the elements in your iterable object, then you could create a reverse() method that, possibly, uses array_reverse() internally. Something like this:-

class Test implements Iterator
{
    private $testing = [0,1,2,3,4,5,6,7,8,9,10];
    private $index = 0;

    public function current()
    {
        return $this->testing[$this->index];
    }

    public function next()
    {
        $this->index ++;
    }

    public function key()
    {
        return $this->index;
    }

    public function valid()
    {
        return isset($this->testing[$this->key()]);
    }

    public function rewind()
    {
        $this->index = 0;
    }

    public function reverse()
    {
        $this->testing = array_reverse($this->testing);
        $this->rewind();
    }
}

$tests = new Test();
var_dump(iterator_to_array($tests));
$tests->reverse();
var_dump(iterator_to_array($tests));

Output:-

array (size=11)
  0 => int 0
  1 => int 1
  2 => int 2
  3 => int 3
  4 => int 4
  5 => int 5
  6 => int 6
  7 => int 7
  8 => int 8
  9 => int 9
  10 => int 10

array (size=11)
  0 => int 10
  1 => int 9
  2 => int 8
  3 => int 7
  4 => int 6
  5 => int 5
  6 => int 4
  7 => int 3
  8 => int 2
  9 => int 1
  10 => int 0

I wrote the code to prove to myself that it would work before posting and thought I might as well throw it into the answer.

这篇关于对待实现迭代器作为数组的PHP类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆