AVELX

Looking for services?

Consultations

You've come so far why not go the whole hog?

Training

You've come so far why not go the whole hog? Join all the fun!

Code Reviews

You've come so far why not go the whole hog?

Push Title

Get unlimited access to source code, repositories, source code and cheatsheets.

What are Generators?
Education, Hacks and Strategy

Free educational material provided by Avelx, ensuring clean and clear delivery on all aspects of programming and architecture

Generator functions are a new way of programming. They allow us to control the execution of a function and even change the state of the function in mid execution.

I’ll compare the generator function with a simple analogy; the traffic system. When you drive your car on the road your controlled by the traffic system. Whilst driving you’ll eventually come to a stop sign or red light, you yield to allow other traffic to flow. But how does this apply to a generator function? Well in the same way you can stop or tell the generator function to yield; allowing for some other process to take place and come back later to execute more commands in the generator function.

For example an old style function when invoked will execute all commands in it’s execution context AKA whatever commands are between the { braces }.

function satNav ( ) { 
	echo 'Start the car.';
	echo '<br/> Drive the car.';
	echo '<br/> First Stop.';
	echo '<br/> Continue driving.';
	echo '<br/> Take a left.';
	echo '<br/> Stop at second stop sign.';
	echo '<br/> Desitnation reached.';
	echo '<br/> Your journey has taken ' . 20 . ' minutes.';
}

When satNav( ) is invoked it’ll produce:

Start the car.
Drive the car.
First Stop.
Continue driving.
Take a left.
Stop at second stop sign.
Desitnation reached.
Your journey has taken 20 minutes.

Notice every single command within the braces or execution context. Also whilst it was excuting the PHP compilers hand’s are tied, it cannot do anything else until the function has finished. That’s the old style function which is fine if we wanted that; But in this case we don’t. For example a sat nav prints each set of instructions as we drive along. So just like we yield in traffic I need my satNav function to yield at some points. This also frees the PHP compiler to do something else. So we need to put in some stop signs or yield signs to tell our function to give way.

In PHP when you add a yield statement within a function PHP automatically knows it’s a generator function. Take this example:

function satNav ( ) { 
	
	echo 'Start the car.';
	echo '<br/> Drive the car.';
	yield '<br/> First Stop.';
	
	echo '<br/> Continue driving.';
	echo '<br/> Take a left.';
	yield '<br/> Stop at second stop sign.';
	
	echo '<br/> Desitnation reached.';
	yield '<br/> Your journey has taken ' . 20 . ' minutes.';
}

So you can think of this as a journey in a car which along the way there are three yield or stop signs. Each time we yield we allow other processes to occur AKA giving way to other processes. Please watch the lecture if the article hasn’t done it for you and then move onto the next lecture for more understanding of generators.

Consultations

You've come so far why not go the whole hog?

Training

You've come so far why not go the whole hog? Join all the fun!

Code Reviews

You've come so far why not go the whole hog?