miniMVC/sys/libraries/session.php

93 lines
1.7 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-17 16:22:39 -04:00
2012-05-14 14:35:57 -04:00
// --------------------------------------------------------------------------
2012-05-03 13:26:09 -04:00
2012-05-22 11:11:36 -04:00
namespace miniMVC;
2012-05-03 13:26:09 -04:00
/**
* Class to improve handling of PHP sessions
*
* @package miniMVC
* @subpackage Libraries
*/
2012-05-21 14:29:36 -04:00
class Session {
2012-05-03 13:26:09 -04:00
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-17 16:22:39 -04:00
$_SESSION['MM_SESSION'] = (isset($_SESSION['MM_SESSION']) ?: [];
$this->sess =& $_SESSION['MM_SESSION'];
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();
}
}
2012-05-17 16:22:39 -04:00
// End of session.php