MemorySession
in package
implements
ISession
In-memory session implementation for testing.
Provides a virtual session that exists entirely in memory. Perfect for testing session-dependent code without actual PHP sessions.
Table of Contents
Interfaces
- ISession
- Interface for session management abstraction.
Constants
- FLASH_KEY = '_flash'
- FLASH_NEW_KEY = '_flash_new'
Properties
Methods
- __construct() : mixed
- Create memory session with optional ID
- all() : array<string|int, mixed>
- Get all session data
- clear() : void
- Clear all session data
- destroy() : bool
- Destroy the session completely
- flash() : void
- Set flash message (available only on next request)
- get() : mixed
- Get value from session
- getFlash() : mixed
- Get flash message (automatically removed after retrieval)
- getId() : string
- Get session ID
- has() : bool
- Check if a key exists in session
- isStarted() : bool
- Check if session is started
- regenerate() : bool
- Regenerate session ID (prevents session fixation attacks)
- remove() : void
- Remove key from session
- set() : void
- Set value in session
- start() : void
- Start the session
- ageFlashData() : void
- Age flash data - move new flash to old, remove old flash
Constants
FLASH_KEY
private
mixed
FLASH_KEY
= '_flash'
FLASH_NEW_KEY
private
mixed
FLASH_NEW_KEY
= '_flash_new'
Properties
$data
private
array<string|int, mixed>
$data
= []
$id
private
string
$id
$started
private
bool
$started
= false
Methods
__construct()
Create memory session with optional ID
public
__construct([string|null $id = null ]) : mixed
Parameters
- $id : string|null = null
-
Session ID (null = generate random)
all()
Get all session data
public
all() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —All session data
clear()
Clear all session data
public
clear() : void
Tags
destroy()
Destroy the session completely
public
destroy() : bool
Tags
Return values
bool —Success status
flash()
Set flash message (available only on next request)
public
flash(string $key, mixed $value) : void
Parameters
- $key : string
-
Flash key
- $value : mixed
-
Flash value
Tags
get()
Get value from session
public
get(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
-
Session key
- $default : mixed = null
-
Default value if key doesn't exist
Tags
Return values
mixed —Session value or default
getFlash()
Get flash message (automatically removed after retrieval)
public
getFlash(string $key[, mixed $default = null ]) : mixed
Parameters
- $key : string
-
Flash key
- $default : mixed = null
-
Default value if flash doesn't exist
Tags
Return values
mixed —Flash value or default
getId()
Get session ID
public
getId() : string
Tags
Return values
string —Session ID
has()
Check if a key exists in session
public
has(string $key) : bool
Parameters
- $key : string
-
Session key
Tags
Return values
bool —True if key exists
isStarted()
Check if session is started
public
isStarted() : bool
Tags
Return values
bool —True if session is active
regenerate()
Regenerate session ID (prevents session fixation attacks)
public
regenerate([bool $deleteOld = true ]) : bool
Parameters
- $deleteOld : bool = true
-
Whether to delete old session file
Tags
Return values
bool —Success status
remove()
Remove key from session
public
remove(string $key) : void
Parameters
- $key : string
-
Session key
Tags
set()
Set value in session
public
set(string $key, mixed $value) : void
Parameters
- $key : string
-
Session key
- $value : mixed
-
Value to store
Tags
start()
Start the session
public
start() : void
Tags
ageFlashData()
Age flash data - move new flash to old, remove old flash
private
ageFlashData() : void