What is the purpose of EmptyIterator?

There is a class in the PHP manual called EmptyIterator

The manual talks about the method EmptyIterator::rewind():

No operation, nothing to do.

And other methods of this class throw exceptions or return false

What is the purpose of an empty iterator?

+4
source share
1 answer

. , , . . , , , . ( , ):

interface Animal {
    public function makeSound();
}

class Dog implements Animal {
    public function makeSound() {
        echo "WOOF!";
    }
}

class Cat implements Animal {
    public function makeSound() {
        echo "MEOW!";
    }
}

class NullAnimal implements Animal { // Null Object Pattern Class
    public function makeSound() {
    }
}

$animalType = 'donkey';
$animal;

switch($animalType) {
    case 'dog' :
        $animal = new Dog();
        break;
    case 'cat' :
        $animal = new Cat();
        break;
    default :
        $animal = new NullAnimal();
}
$animal->makeSound(); // won't make any sound bcz animal is 'donkey'

Null Object Pattern Class, . . , , - .

+4

Source: https://habr.com/ru/post/1608816/


All Articles