When a closure is not necessary
For a while, after being exposed to a lot of javascript during a project for a social gaming company it's been confused in my mind that closures in programming are useful for retaining data statically. This now makes sense because javascript isn't like other languages and it lacks the static keyword so there is no alternative.
I eventually realised I needed this, simplest of functions for returning prefixes for SQL where clauses.
function whereClause($query) {
static $i = 0;
$prefix = 'where';
if ($i > 0) {
$prefix = 'and';
}
$i++;
return $prefix.' '.$query;
}
echo whereClause('foo = bar');
echo whereClause('foo = bar');