Session storage

The session information is stored in the global session space. Spoova provides some useful methods to manage how data are stored or accessed from global session. These methods are discussed below:
This method is used to save session data. It requires a session key followed by the session value which is expected to be saved into the session key which can be of any data type.

Syntax: Session::save()
 Session::save($key, $value)
In the code above, the $key is usually a string key while the value supplied can be of any data type. Once this method is called, the key will be used to save the data into the global session space.
The Session::value() method is used to obtain the value of a specified key from the global session space. Although it requires a key to know which value it is required to return, if not key is specified, the entire session content is returned.

Syntax: Session::value()
  Session::value($key?, $subkey?)
      $key    : session key whose value is expected to be returned
      $subkey : a subkey of the value of $key if the value is an array.
THe $key above is used to fetch the value of an existing key in the storage. If $subkey is specified, and the value of $key supplied is an array, then $subkey is assumed to be a subkey of the value of $key. This means that the value of the subkey will be returned instead. An example is shown below:
Set and fetch a data.

  $data1 = 'foobaz';

  $data2 = [
    'name1' => 'foo', 
    'name2' => 'bar', 
    'name3' => 'baz', 


  Session::save('data1', $data1);

  Session::save('data2', $data2);

  $session_data  = Session::value(); // returns both data1 and data2 

  $session_data1 = Session::value('data1'); // foobaz 

  $session_data2 = Session::value('data2'); // ['name1' => 'foo', 'name2' => 'bar', 'name3' => 'baz']
  $session_data1 = Session::value('data2'); // ['name1' => 'foo', 'name2' => 'bar', 'name3' => 'baz']

  $session_name1 = Session::value('data2', 'name1'); // foo

  $session_name2 = Session::value('data2', 'name2'); // bar
  $session_names = Session::value('data2', ['name1','name3']); // ['foo', 'baz']
The code above best explains how the Session::value() works. Once we save a data into the session, we can obtain it by specifying the storage key used to store the data. The first result above shows that when the value is not specified, all the data stored is returned back. Also we can see that when the subkey argument is supplied, it only works when the data stored is an array that contains the the specified subkey value. Hence, the value of the subkey is returned.
The overwrite() method is used to overwrite the entire value of a session. This means that the session value is entirely replaced by the new value supplied. Usually, it is not advisable to overwrite the entire value of the global session data. If there is a need to modify the entire session data, the best way to perform this is to fetch the entire session value first. The value fetched can then be modified and then saved back using the overwrite() method. In this way, we are sure that no data will be lost.

Syntax: Session::overwrite()
   where $newvalue: a new value that overides the previous session value.  
An example below best address how to properly overwrite the session global space
Sample: Session::overwrite()
  $value = Session::value();// fetch current session value

  $value['some_key'] = "new_value"; // perform some modification

  Session::overwrite($value); // overwrite entire session value

This method is used to remove the a key from the global session space. It can also be used to remove the entire value of a session.

Syntax: Session::remove()
  Session::remove($key?, $subkey?);
    $key    : key expected to be removed from session  
    $subkey : subkey that is expected to be removed from a key in database 
Remove a data

  $data1 = ['name'=>'foobaz'];

  $data2 = [
    'name1' => 'foo', 
    'name2' => 'bar', 


  Session::save('data1', $data1);
  Session::save('data2', $data2);

  Session::remove('data2', 'name2'); 
  var_dump(Session::value()); //[ ['name'=>'foobaz'], ['name1' => 'foo']]

  var_dump(Session::value('data1')); // ['name1' => 'foobaz']

  var_dump(Session::value('data2')); // ['name1' => 'foo']

  Session::remove(); // remove entire session value 

  var_dump(Session::value()); // empty
The sample above shows that a direct subkey of an array saved data can be removed from a key when both the key and the subkey is provided. However, if only the key is supplied, then only the key is removed. In the event that no key was supplied, the remove() method will assume to remove the entire sesion data.