Using SplStack class from SPL

If we are not interested in implementing our own version of a stack, we can use the existing SPL implementation for stacks. It is very easy to use and requires minimal code to write. As we already know, SplStack uses SplDoublyLinkedList. It has all possible operations to push, pop, move forward, backward, shift, unshift, and so on. In order to implement the same example we saw previously, we have to write the following lines:

$books = new SplStack(); 
$books->push("Introduction to PHP7");
$books->push("Mastering JavaScript");
$books->push("MySQL Workbench tutorial");
echo $books->pop() . " ";
echo $books->top() . " ";

Yes, it is this simple to build a stack using the SplStack class. It is up to us to decide whether we want to implement it using a PHP array, a linked list, or a built-in class, such as SplStack.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset