Introduction to Zend Framework

 Learning Zend Framework


 Zend Framework Reference

  •  Zend_Gdata
  •  Zend_Http
  •  Zend_InfoCard
  •  Zend_Json
  •  Zend_Layout
  •  Zend_Ldap
  •  Zend_Loader
  •  Zend_Locale
  •  Zend_Log
  •  Zend_Mail
  •  Zend_Markup
  •  Zend_Measure
  •  Zend_Memory
  •  Zend_Mime
  •  Zend_Navigation
  •  Zend_Oauth
  •  Zend_OpenId
  •  Zend_Paginator
  •  Zend_Pdf
  •  Zend_ProgressBar
  •  Zend_Queue
  •  Zend_Reflection
  •  Zend_Registry
  •  Zend_Rest

  •  Zend_Search_Lucene
  •  Zend_Serializer
  •  Zend_Server
  •  Zend_Service
  •  Zend_Session
  •  Zend_Soap
  •  Zend_Tag
  •  Zend_Test
  •  Zend_Text
  •  Zend_TimeSync
  •  Zend_Tool
  •  Zend_Tool_Framework
  •  Zend_Tool_Project
  •  Zend_Translate
  •  Zend_Uri
  •  Zend_Validate
  •  Zend_Version
  •  Zend_View
  •  Zend_Wildfire
  •  Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Update 2011-11-16 - Revision 24438 - Version ZF 1.11.x

    64.2. Basic Usage

    Zend_Session_Namespace instances provide the primary API for manipulating session data in the Zend Framework. Namespaces are used to segregate all session data, although a default namespace exists for those who only want one namespace for all their session data. Zend_Session utilizes ext/session and its special $_SESSION superglobal as the storage mechanism for session state data. while $_SESSION is still available in PHP's global namespace, developers should refrain from directly accessing it, so that Zend_Session and Zend_Session_Namespace can most effectively and securely provide its suite of session related functionality.

    Each instance of Zend_Session_Namespace corresponds to an entry of the $_SESSION superglobal array, where the namespace is used as the key.

    $myNamespace = new Zend_Session_Namespace('myNamespace');

    // $myNamespace corresponds to $_SESSION['myNamespace']

    It is possible to use Zend_Session in conjunction with other code that uses $_SESSION directly. To avoid problems, however, it is highly recommended that such code only uses parts of $_SESSION that do not correspond to instances of Zend_Session_Namespace.

    64.2.1. Tutorial Examples

    If no namespace is specified when instantiating Zend_Session_Namespace, all data will be transparently stored in a namespace called "Default". Zend_Session is not intended to work directly on the contents of session namespace containers. Instead, we use Zend_Session_Namespace. The example below demonstrates use of this default namespace, showing how to count the number of client requests during a session:

    Example 64.1. Counting Page Views

    $defaultNamespace = new Zend_Session_Namespace('Default');

    if (isset(
    $defaultNamespace->numberOfPageRequests)) {
    // this will increment for each page load.
    } else {
    $defaultNamespace->numberOfPageRequests 1// first time

    "Page requests this session: ",

    When multiple modules use instances of Zend_Session_Namespace having different namespaces, each module obtains data encapsulation for its session data. The Zend_Session_Namespace constructor can be passed an optional $namespace argument, which allows developers to partition session data into separate namespaces. Namespacing provides an effective and popular way to secure session state data against changes due to accidental naming collisions.

    Namespace names are restricted to character sequences represented as non-empty PHP strings that do not begin with an underscore ("_") character. Only core components included in Zend Framework should use namespace names starting with "Zend".

    Example 64.2. New Way: Namespaces Avoid Collisions

    // in the Zend_Auth component
    $authNamespace = new Zend_Session_Namespace('Zend_Auth');
    $authNamespace->user "myusername";

    // in a web services component
    $webServiceNamespace = new Zend_Session_Namespace('Some_Web_Service');
    $webServiceNamespace->user "mywebusername";

    The example above achieves the same effect as the code below, except that the session objects above preserve encapsulation of session data within their respective namespaces.

    Example 64.3. Old Way: PHP Session Access

    $_SESSION['Zend_Auth']['user'] = "myusername";
    $_SESSION['Some_Web_Service']['user'] = "mywebusername";

    64.2.2. Iterating Over Session Namespaces

    Zend_Session_Namespace provides the full IteratorAggregate interface, including support for the foreach statement:

    Example 64.4. Session Iteration

    $aNamespace =

    foreach (
    $aNamespace as $index => $value) {
    "aNamespace->$index = '$value';\n";

    64.2.3. Accessors for Session Namespaces

    Zend_Session_Namespace implements the __get(), __set(), __isset(), and __unset() magic methods, which should not be invoked directly, except from within a subclass. Instead, the normal operators automatically invoke these methods, such as in the following example:

    Example 64.5. Accessing Session Data

    $namespace = new Zend_Session_Namespace(); // default namespace

    $namespace->foo 100;

    "\$namespace->foo = $namespace->foo\n";

    if (!isset(
    $namespace->bar)) {
    "\$namespace->bar not set\n";


    digg delicious meneame google twitter technorati facebook