Skip to content

Instantly share code, notes, and snippets.

@12Rain12
Created June 19, 2012 06:22
Show Gist options
  • Save 12Rain12/2952581 to your computer and use it in GitHub Desktop.
Save 12Rain12/2952581 to your computer and use it in GitHub Desktop.
class Session
<?php
/**
* Класс работы с сессиями
*
* Использование
* session::getInstance()->start();
* dump(session::getInstance()->get('qwer'));
* session::getInstance()->set('qwer', 'qwer');
* 1 параметр $key - наименование сессии
* 2 параметр $value - значени сессии
*
*/
class Session
{
/**
* @param $instance переменная предназначеная для
* хранения экземпляра класса Session
*/
protected static $instance;
/**
* @param $session_start boolean идентификатор работы сессии
*/
private $session_start = FALSE;
private function __construct()
{
}
public static function getInstance()
{
return (null !== self::$instance) ? self::$instance : (self::$instance = new self());
}
/**
* функция старта сессии
* Возращает true если сессия стартовала успешно
*/
public function start()
{
if($this->session_start){
return true;
}
if(!session_start()){
throw new Exception('Error session start');
}
$this->session_start = true;
return true;
}
/**
* Возвращает значение элемента $key
* @param $key string имя сессии
*/
public function getSession($key)
{
$this->start();
return (!empty($_SESSION[$key])) ? $_SESSION[$key] : false;
}
/**
* Устанавливаем элементу $key значение $value
* @param $key string имя сессии
* @param $value string значение сессии
*/
public function setSession($key, $value)
{
$this->start();
$_SESSION[$key] = $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment