miniMVC/sys/libraries/session.php

95 lines
1.8 KiB
PHP
Raw Normal View History

2012-05-03 13:26:09 -04:00
<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @package miniMVC
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
* @link https://github.com/timw4mail/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
2012-05-14 14:35:57 -04:00
/**
* Define a session id to namespace sessions
*/
define(SESS_ID, 'MM_SESSION');
// --------------------------------------------------------------------------
2012-05-03 13:26:09 -04:00
/**
* Class to improve handling of PHP sessions
*
* @package miniMVC
* @subpackage Libraries
*/
class MM_Session{
2012-05-03 16:07:40 -04:00
// Combine traits for more dynamic behavior
// but use Singleton for __invoke, so calling
// that method does not create a new instance
2012-05-03 13:26:09 -04:00
use JSObject, Singleton
{
Singleton::__invoke insteadof JSObject;
}
2012-05-03 16:07:40 -04:00
/**
* Reference to session superglobal
*
* @var array
*/
protected $sess = [];
2012-05-03 13:26:09 -04:00
/**
* Start a session
*/
protected function __construct()
{
session_start();
2012-05-03 16:07:40 -04:00
// Save a reference to the session for later access
2012-05-14 14:35:57 -04:00
$_SESSION[SESS_ID] = (isset($_SESSION[SESS_ID]) ?: [];
$this->sess =& $_SESSION[SESS_ID];
2012-05-03 13:26:09 -04:00
}
// --------------------------------------------------------------------------
/**
* Set a session value
*
* @param string $key
* @param mixed $val
* @return void
*/
public function __set($key, $val)
{
2012-05-03 16:07:40 -04:00
$this->sess[$key] = $val;
2012-05-03 13:26:09 -04:00
}
// --------------------------------------------------------------------------
/**
* Retreive a session value
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
2012-05-03 16:07:40 -04:00
return $this->sess[$key];
2012-05-03 13:26:09 -04:00
}
// --------------------------------------------------------------------------
/**
* Destroy a session
*
* @return void
*/
public function destroy()
{
sess_destroy();
}
}
// End of php_session.php