Fork me on GitHub

Persistant stack class for PHP

I made a simple class that provides a simple SQLite-based persistant FIFO stack class for PHP 5 (similar conceptually to Stack::Persistent in Perl).

A number of times I've written Drupal scripts that run out of memory while trying to process large amounts of nodes (changing taxonomy, etc.). Adding them to a persistant stack, then doing them in batches, is an easy way to remedy this.

Example usage:

// Open or create stack: if you don't specify a filename, 'stack.db' will be used
$stack = new PersistentStack('my_stuff.db');
 
// Add simple things to a named stack
$stack->push('clothes', 'socks');
$stack->push('clothes', 'pants');
 
// Add something complete to another named stack
$stack->push('books', array(
  'title'  => 'The Tin Drum',
  'author' => 'Gunter Grass'
));
 
// Output the top of a stack without removing it from the stack
print $stack->last('clothes');
 
// Retrieve the top of a stack, removing it from the stack
$last_item = $stack->pop('clothes');

Documentation and code at GitHub.