A stack is a simple algorithm normally implemented as Last In First Out (LIFO). Think of a stack of books sitting on a library table. When the librarian goes to restore the books to their place, the topmost book is processed first, and so on in order, until the book at the bottom of the stack has been replaced. The topmost book was the last one to be placed on the stack, thus last in first out.
In programming terms, a stack is used to temporarily store information. The retrieval order facilitates retrieving the most recent item first.
Application\Generic\Stack. The core logic is encapsulated in an SPL class, SplStack:namespace Application\Generic;
use SplStack;
class Stack
{
// code
}SplStack instance:protected $stack;
public function __construct()
{
$this->stack = new SplStack();
}push() and pop() methods:public function push($message)
{
$this->stack->push($message);
}
public function pop()
{
return $this->stack->pop();
}__invoke() that returns an instance of the stack property. This allows us to use the object in a direct function call:public function __invoke()
{
return $this->stack;
}One possible use for a stack is to store messages. In the case of messages, it is usually desirable to retrieve the latest first, thus it is a perfect use case for a stack. Define the Application\Generic\Stack class as discussed in this recipe. Next, define a calling program that sets up autoloading and creates an instance of the stack:
<?php // setup class autoloading require __DIR__ . '/../Application/Autoload/Loader.php'; Application\Autoload\Loader::init(__DIR__ . '/..'); use Application\Generic\Stack; $stack = new Stack();
To do something with the stack, store a series of messages. As you would most likely store messages at different points in your application, you can use sleep() to simulate other code running:
echo 'Do Something ... ' . PHP_EOL;
$stack->push('1st Message: ' . date('H:i:s'));
sleep(3);
echo 'Do Something Else ... ' . PHP_EOL;
$stack->push('2nd Message: ' . date('H:i:s'));
sleep(3);
echo 'Do Something Else Again ... ' . PHP_EOL;
$stack->push('3rd Message: ' . date('H:i:s'));
sleep(3);Finally, simply iterate through the stack to retrieve messages. Note that you can call the stack object as if it were a function, which returns the SplStack instance:
echo 'What Time Is It?' . PHP_EOL;
foreach ($stack() as $item) {
echo $item . PHP_EOL;
}Here is the expected output:
