PHP data structures: spl Stack

Why no array?

Sometimes (yes in web development too) you need to optimize your code for speed and memory usage. PHP is simplified by design and heavily depends on array usage. You can speed things up a little using proper data structures for the job. Luckily the language provides build in classes and interfaces for the most frequent data structures, all wrapped in SPL library. Implementing Stack makes your code cleaner and more readable. We are going to utilize SplStack abstract class, and build Stack to address very common problem in every day programming which is clean dishes pile.


Using Stack in PHP is very easy. Your class need to extend SplStack abstract class, and you are good to go. Overriding or building custom methods like in the example above is only needed if there is additional functionality required.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditEmail this to someoneShare on StumbleUpon