Jul.09

Yield, collaborate and listen

I’m back with a brand new edition.

PHP has introduced a new powerful keywords to use with generators. Instead of returning one variable for a generator, you can now stack what is returned. This may seem like a very small addition to such a vast language, but it is extremely helpful for generators.

A generator function looks just like a normal function, except that instead of returning a value, a generator yields as many values as it needs to.

This allows us to turn

function sleeper() {
	$data = array();
	$data[] = time();
	sleep(2);
	$data[] = time();
	return $data;
}

INTO :

function sleeper() {
	yield time();
	sleep(2);
	yield time();
}
Programming,PHP

Leave a comment