SessionModel
in package
implements
SessionModelInterface
Abstract session management model.
Provides a standardized interface for storing and retrieving user session data. Extend this class and implement getUser() to define custom user data retrieval.
Usage:
class UserSession extends \X\Model\SessionModel {
protected static function getUser(string $id): array {
return (new UserModel())->get_by_id((int)$id);
}
}
// Set user session
UserSession::set($userId);
// Get session data
$user = UserSession::get();
Table of Contents
Interfaces
- SessionModelInterface
- Interface for session management models.
Constants
- SESSION_NAME = 'user'
- Key name of the session to store user information.
Methods
- get() : stdClass|mixed|null
- Get session data.
- isset() : bool
- Check if a user session exists.
- set() : string
- Set session data.
- unset() : string
- Destroy the current user session.
- getUser() : array<string|int, mixed>
- Retrieve user data by ID for session storage.
Constants
SESSION_NAME
Key name of the session to store user information.
public
string
SESSION_NAME
= 'user'
Methods
get()
Get session data.
public
final static get([string|null $field = null ]) : stdClass|mixed|null
Returns all session data as an object, or a single field value if a field name is specified.
Parameters
- $field : string|null = null
-
Field name to retrieve. Null returns all data.
Return values
stdClass|mixed|null —All session data, a single field value, or null if no session.
isset()
Check if a user session exists.
public
final static isset() : bool
Return values
bool —True if session data is set.
set()
Set session data.
public
final static set(string $id[, mixed $value = null ]) : string
With one argument, loads full user data into session via getUser(). With two arguments, updates a single session field.
Parameters
- $id : string
-
User ID (1 arg) or field name (2 args).
- $value : mixed = null
-
Field value when updating a single field.
Tags
Return values
string —Called class name for method chaining.
unset()
Destroy the current user session.
public
final static unset() : string
Return values
string —Called class name for method chaining.
getUser()
Retrieve user data by ID for session storage.
protected
abstract static getUser(string $id) : array<string|int, mixed>
Subclasses must implement this to define how user data is fetched (e.g., from a database model).
Parameters
- $id : string
-
User ID.
Return values
array<string|int, mixed> —User data to store in session.