What is the EmptyIterator class for?

7

In the PHP documentation, there is a class called EmptyIterator

When I look at the documentation for the EmptyIterator::rewind() method, it says:

  

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

And the other methods throw exceptions or return FALSE .

What is the purpose of having an empty% in the manual? For if I had to extend this class, the inherited methods would serve no purpose!?!?

    
asked by anonymous 03.06.2015 / 13:34

1 answer

3

I think the solution to this question can be found in this response given by @bigown, here in SOPT.

What is the Null Object standard for?

EmptyIterator was probably created as a way to make this object meet the requirements of the Iterator interface, without performing any operation with the contract methods.

That is, it would fit seamlessly into the Null Object pattern.

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

This is a very simple example, which does not fully demonstrate the effectiveness of this method, but rather exemplifies what the Null Object standard represents.

    
10.10.2015 / 14:59