Zend Framework: Writing and Reading Session Data Using Zend Session Namespace

October 5th, 2012 by laeeq | No comments

Today I have tried with Session in Zend Framework. Actually Session handling is very simple in Zend Framework. It’s handled very efficiently using Object Oriented Programming. In this article I’m going to discuss some useful techniques of using Zend Framework Session and Session namespace. In Zend framework, you can use both Zend_Session and Zend_Session_Namespace which extends abstract class Zend_Session_Abstract. So these two inherited methods are available in Zend_Session_Abstract automatically.

You can find all functions related to session handling at Zend/Session/Abstract.php. You can use either Zend_Session or Zend_Session_Namespace class object, but I would recommend Zend_Session_Namespace class object which is more efficient. You can instantiate session using Zend_Session_Namespace class object as below:

  1. $mysession = new Zend_Session_Namespace(’Namespace’);

The argument for namespace is optional, but it’s better to pass it. if you don’t pass it, Zend Session Namespace will assign its name a string “default”.

To store values in session variable, you will need to do the following.

  1. $mysession->fruits = ‘Apple’;

Below is the example code to retrieve values from session.

  1. $yoursession = new Zend_Session_Namespace(’Namespace’);
  2. $fruits = $yoursession->fruits;

If you want to go deep, you can get many useful functions in Zend_Session_Abstract.php, Zend_Session_Nampspace.php, and Zend_Session.php.

 

You can subscribe to PHPZAG.COM posts by Email

 

 

 

  1. No comments yet.
  1. No trackbacks yet.