Home > PHP, PHP Class Examples, PHP Tutorials > Stack Implementation in PHP5 – Stack Class

Stack Implementation in PHP5 – Stack Class

November 14th, 2007 admin Leave a comment Go to comments

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)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – Magic Methods – __toString() method
  9. PHP5 Tutorial – Magic Methods – __get() and __set()
  10. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  11. PHP5 Tutorial – Magic Methods – __call() method
  12. PHP5 Tutorial – Magic Methods – __autoload() method
  13. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  14. PHP5 Tutorial – Magic Methods – __clone() method

Categories: PHP, PHP Class Examples, PHP Tutorials Tags:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. smiley
    November 14th, 2007 at 19:12 | #1

    why reinvent the wheel?

    user array_push() and array_pop()

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )