Magento introduces grouping to manage session data for different usage that differentiates it from its counterparts. Let’s dig into the details. All session data in Magento is stored in global variable $_SESSION
, an array from programming view, and categorizes them into independent groups with each group represented by an array encapsulated by a session class.
Magento by default is equipped with three session classes for core, customer and checkout.
Mage::getSingelton('core/session')
,
Mage::getSingleton('customer/session')
,
Mage::getSingleton('checkout/session')
are three session functions used
in Magento.
$_SESSION
variable finally has the form of the following:
$_SESSION=array('core'=>array(...), 'customer'=>array(...), 'checkout'=>array(...),...);
we should avoid to directly operate on $_SESSION
variable, and instead act in Magento’s way, like Mage::getSingleton('core/session')-getXXX()
and Mage::getSingleton('core/session')->setXXX()
.
If a customized session class is needed, it is required to inherit Mage_Core_Model_Session_Abstract
. Below is an example:
class Company_Module_Model_Session extends Mage_Core_Model_Session_Abstract
{
public function __construct() {
$this->init('module');
}
}
And then we can use this customized session class by calling Mage::getSingleton('mgwishlist/session')
, just as any other session classes in Magento.
Particularly, PHP session can only keep values of basic data types, such as int
, bool
, string
and etc, so serialization/deserialization is frequently touched.