2
0

Zend_Session-BasicUsage.xml 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.session.basic_usage">
  4. <title>Basic Usage</title>
  5. <para>
  6. <classname>Zend_Session_Namespace</classname> instances provide the primary
  7. <acronym>API</acronym> for manipulating session data in the Zend Framework. Namespaces are
  8. used to segregate all session data, although a default namespace exists for those who only
  9. want one namespace for all their session data. <classname>Zend_Session</classname> utilizes
  10. ext/session and its special <varname>$_SESSION</varname> superglobal as the storage
  11. mechanism for session state data. while <varname>$_SESSION</varname> is still available in
  12. <acronym>PHP</acronym>'s global namespace, developers should refrain from directly accessing
  13. it, so that <classname>Zend_Session</classname> and
  14. <classname>Zend_Session_Namespace</classname> can most effectively and securely provide its
  15. suite of session related functionality.
  16. </para>
  17. <para>
  18. Each instance of <classname>Zend_Session_Namespace</classname> corresponds to an entry of
  19. the <varname>$_SESSION</varname> superglobal array, where the namespace is used as the key.
  20. </para>
  21. <programlisting language="php"><![CDATA[
  22. $myNamespace = new Zend_Session_Namespace('myNamespace');
  23. // $myNamespace corresponds to $_SESSION['myNamespace']
  24. ]]></programlisting>
  25. <para>
  26. It is possible to use <classname>Zend_Session</classname> in conjunction with other code
  27. that uses <varname>$_SESSION</varname> directly. To avoid problems, however, it is highly
  28. recommended that such code only uses parts of <varname>$_SESSION</varname> that do not
  29. correspond to instances of <classname>Zend_Session_Namespace</classname>.
  30. </para>
  31. <sect2 id="zend.session.basic_usage.basic_examples">
  32. <title>Tutorial Examples</title>
  33. <para>
  34. If no namespace is specified when instantiating
  35. <classname>Zend_Session_Namespace</classname>, all data will be transparently stored in
  36. a namespace called "<code>Default</code>". <classname>Zend_Session</classname> is not
  37. intended to work directly on the contents of session namespace containers. Instead, we
  38. use <classname>Zend_Session_Namespace</classname>. The example below demonstrates use of
  39. this default namespace, showing how to count the number of client requests during a
  40. session:
  41. </para>
  42. <example id="zend.session.basic_usage.basic_examples.example.counting_page_views">
  43. <title>Counting Page Views</title>
  44. <programlisting language="php"><![CDATA[
  45. $defaultNamespace = new Zend_Session_Namespace('Default');
  46. if (isset($defaultNamespace->numberOfPageRequests)) {
  47. // this will increment for each page load.
  48. $defaultNamespace->numberOfPageRequests++;
  49. } else {
  50. $defaultNamespace->numberOfPageRequests = 1; // first time
  51. }
  52. echo "Page requests this session: ",
  53. $defaultNamespace->numberOfPageRequests;
  54. ]]></programlisting>
  55. </example>
  56. <para>
  57. When multiple modules use instances of <classname>Zend_Session_Namespace</classname>
  58. having different namespaces, each module obtains data encapsulation for its session
  59. data. The <classname>Zend_Session_Namespace</classname> constructor can be passed an
  60. optional <varname>$namespace</varname> argument, which allows developers to partition
  61. session data into separate namespaces. Namespacing provides an effective and popular way
  62. to secure session state data against changes due to accidental naming collisions.
  63. </para>
  64. <para>
  65. Namespace names are restricted to character sequences represented as non-empty
  66. <acronym>PHP</acronym> strings that do not begin with an underscore ("<code>_</code>")
  67. character. Only core components included in Zend Framework should use namespace names
  68. starting with "<code>Zend</code>".
  69. </para>
  70. <example id="zend.session.basic_usage.basic_examples.example.namespaces.new">
  71. <title>New Way: Namespaces Avoid Collisions</title>
  72. <programlisting language="php"><![CDATA[
  73. // in the Zend_Auth component
  74. $authNamespace = new Zend_Session_Namespace('Zend_Auth');
  75. $authNamespace->user = "myusername";
  76. // in a web services component
  77. $webServiceNamespace = new Zend_Session_Namespace('Some_Web_Service');
  78. $webServiceNamespace->user = "mywebusername";
  79. ]]></programlisting>
  80. </example>
  81. <para>
  82. The example above achieves the same effect as the code below, except that the session
  83. objects above preserve encapsulation of session data within their respective namespaces.
  84. </para>
  85. <example id="zend.session.basic_usage.basic_examples.example.namespaces.old">
  86. <title>Old Way: PHP Session Access</title>
  87. <programlisting language="php"><![CDATA[
  88. $_SESSION['Zend_Auth']['user'] = "myusername";
  89. $_SESSION['Some_Web_Service']['user'] = "mywebusername";
  90. ]]></programlisting>
  91. </example>
  92. </sect2>
  93. <sect2 id="zend.session.basic_usage.iteration">
  94. <title>Iterating Over Session Namespaces</title>
  95. <para>
  96. <classname>Zend_Session_Namespace</classname> provides the full <ulink
  97. url="http://www.php.net/~helly/php/ext/spl/interfaceIteratorAggregate.html">IteratorAggregate
  98. interface</ulink>, including support for the <code>foreach</code> statement:
  99. </para>
  100. <example id="zend.session.basic_usage.iteration.example">
  101. <title>Session Iteration</title>
  102. <programlisting language="php"><![CDATA[
  103. $aNamespace =
  104. new Zend_Session_Namespace('some_namespace_with_data_present');
  105. foreach ($aNamespace as $index => $value) {
  106. echo "aNamespace->$index = '$value';\n";
  107. }
  108. ]]></programlisting>
  109. </example>
  110. </sect2>
  111. <sect2 id="zend.session.basic_usage.accessors">
  112. <title>Accessors for Session Namespaces</title>
  113. <para>
  114. <classname>Zend_Session_Namespace</classname> implements the
  115. <methodname>__get()</methodname>, <methodname>__set()</methodname>,
  116. <methodname>__isset()</methodname>, and <methodname>__unset()</methodname> <ulink
  117. url="http://www.php.net/manual/en/language.oop5.overloading.php">magic
  118. methods</ulink>, which should not be invoked directly, except from within a
  119. subclass. Instead, the normal operators automatically invoke these methods, such as in
  120. the following example:
  121. </para>
  122. <example id="zend.session.basic_usage.accessors.example">
  123. <title>Accessing Session Data</title>
  124. <programlisting language="php"><![CDATA[
  125. $namespace = new Zend_Session_Namespace(); // default namespace
  126. $namespace->foo = 100;
  127. echo "\$namespace->foo = $namespace->foo\n";
  128. if (!isset($namespace->bar)) {
  129. echo "\$namespace->bar not set\n";
  130. }
  131. unset($namespace->foo);
  132. ]]></programlisting>
  133. </example>
  134. </sect2>
  135. </sect1>