What is the Emptyiterator class for?

Asked

Viewed 104 times

8

In the documentation of PHP, has a class called EmptyIterator

When I look at the method documentation EmptyIterator::rewind(), there is written:

No Operation, Nothing to do. (No operation, nothing to do)

And the other methods make exceptions or return FALSE.

What’s the point of having one iterator empty in the manual? For if I had to extend this class, the inherited methods would serve no purpose !?!?

  • 1

    Ah, I know it’s no use.

  • 1

    http://stackoverflow.com/questions/32762768/what-is-the-purpose-of-emptyiterator

  • 1

    Why don’t you answer the question, and accept the answer, since you already know what it is ?

  • thanks, @Edilson. When I have time I’ll do it.

1 answer

4


I believe that the solution to this question can be found in the answer given by @Maniero, here at SOPT, What is the Null Object standard for?

Probably the EmptyIterator was created as a way to make this object meet the requirements of the interface Iterator, without carrying out any transaction with the contract methods.

I mean, it would fit perfectly into the Pattern Null Object.

Example:

function required_iterator(Iterator $it)
{
   // Faz um paranauê aqui
}


required_iterator(new ArrayIterator([1, 2, 3]))

//Atende o requisito da função: Uma classe que implemente iterator

required_iterator(new EmptyIterator);

It is a very simple example, which does not fully show the effectiveness of this method, but exemplifies what the Null Object pattern represents.

  • +1 to @bigown, even if he didn’t reply here :_:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.