30.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.

30.2.1. Tutorial Examples

If no namespace is specified when instantiating Zend_Session, 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 times a user views pages on your website. To test this example, add the following code to your ZF bootstrap area:

مثال 30.1. Counting Page Views

<?php
    require_once 'Zend/Session.php';

    $defaultNamespace = new Zend_Session_Namespace('Default');

    // use the magic method __isset() in Zend_Session_Namespace:
    if (isset($defaultNamespace->numberOfPageRequests)) {
        $defaultNamespace->numberOfPageRequests++; // this will increment for each page load.
    } else {
        $defaultNamespace->numberOfPageRequests = 1; // first time
    }

    echo "Page requests this session: ", $defaultNamespace->numberOfPageRequests;
?>

One of the many benefits of Zend_Session_Namespace results when multiple modules use Zend_Session_Namespace and obtain data encapsulation for their session data. Zend_Session can be passed an optional $namespace argument in the constructor, which allows other components, modules, and developer specific code to be assured that their data is protected by a partition between data areas used by other components, modules, and developer code. Namespacing provides an effective and popular way to "secure" a subset of session state data against accidental changes. 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 the Zend Framework should use namespace names starting with 'Zend_'.

مثال 30.2. New Way: Namespaces Avoid Collisions

<?php
    // in the Zend_Auth component
    require_once 'Zend/Session.php';
    $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.

مثال 30.3. Old Way: PHP Session Access

<?php
    $_SESSION['Zend_Auth']['user'] = "myusername";
    $_SESSION['Some_Web_Service']['user'] = "mywebusername";
?>

30.2.2. Iterating Over Session Namespaces

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

مثال 30.4. Session Iteration

<?php
    // Zend_Session is iteratable
    require_once 'Zend/Session.php';
    $aNamespace = new Zend_Session_Namespace('some_namespace_with_data_present');
    foreach ($aNamespace as $index => $value) {
        echo "aNamespace->$index = '$value';\n";
    }
?>

30.2.3. Accessors for Session Namespaces

The usual accessors are available, via the __set(), __unset(), __isset(), and __get() magic methods. The magic methods should not be used directly, except from within a subclass of Zend_Session. Instead, use the normal operators to invoke these magic methods, such as:

مثال 30.5. Accessing Session Data

<?php
            $object->property = $value; echo
            (isset($object->property) ? 'set' : 'unset');
?>