1

I can write a generator returning a single element:

function MyGen() {
   yield 1;
}

In a derived class I want to write a generator that never returns any element:

function MyGen() {

}

An empty function is not a generator and will fail if passed as aforeach argument. So - How can I write a generator which will always return no elements?

powerpete
  • 2,663
  • 2
  • 23
  • 49

1 Answers1

3

This seems to work (php7)

function MyGen() {
    yield from [];
}

See http://php.net/manual/en/language.generators.syntax.php

Chris Lear
  • 6,592
  • 1
  • 18
  • 26