Stack Implementation in PHP5 - Stack Class
Posted on November 14th, 2007 by admin
Refer to the code below which is a PHP5 Stack Class - an implementation of Stacks. You are free to use it in your programs.
class Stack { private $stk = array(); public function __construct() { } public function push($data) { array_push($this->stk, $data); } public function pop() { return array_pop($this->stk); } } $s = new Stack(); $s->push("Sunil"); $s->push("Bhatia"); echo $s->pop(); echo "\n"; echo $s->pop();
Please fee free to leave comments if you have any questions or suggestions. Also don’t forget to subscribe to the newsletter - to keep yourself updated as and when new posts are made live. Subscribe Below:
PHP5 Tutorial - Object Oriented Programming (OOPS)
- PHP5 Tutorial - Learn to create a PHP5 Class
- PHP5 Tutorial - Learn to Create a PHP5 Class Object
- PHP5 Tutorial - Defining Attributes of a PHP5 Class
- PHP5 Tutorial - Defining Methods of a PHP5 Class
- PHP5 Tutorial - Creating a PHP5 Constructor __construct()
- PHP5 Tutorial OOPS - Creating a PHP5 Destructor __destruct()
- PHP5 Tutorial OOPS - PHP5 Class Access Specifiers - public, private and protected
- PHP5 Tutorial - Magic Methods - __toString() method
- PHP5 Tutorial - Magic Methods - __get() and __set()
- PHP5 Tutorial - Magic Methods - __isset() and __unset()
- PHP5 Tutorial - Magic Methods - __call() method
- PHP5 Tutorial - Magic Methods - __autoload() method
- PHP5 Tutorial - Magic Methods - __sleep() and __wakeup()
- PHP5 Tutorial - Magic Methods - __clone() method
Filed under: PHP, PHP Class Examples, PHP Tutorials
why reinvent the wheel?
user array_push() and array_pop()