Sex
in package
PHPEz session management helper.
Session => Sess => Sex (not what you're thinking 😄)
Provides a namespaced, type-safe interface to PHP sessions with automatic initialization and lazy session starting. Prevents "headers already sent" errors by deferring session_start() until actually needed.
Key features:
- Lazy initialization: Session starts only when accessed, not on instantiation
- Namespacing: All keys are prefixed with a namespace root key
- Fluent API: All mutation methods return $this for chaining
- Global instance: Sex::global() or $SEX provides app-wide access
- Type hints: Works seamlessly with Model::toSex() / Model::fromSex()
Naming convention:
- Root key: 'phpez_shared_sex'
- Stored keys: 'phpez_shared_sex:keyname'
- Enables safe coexistence with other session data
Usage:
// Initialize in bootstrap (index.php):
Sex::initGlobal();
// Instance or static global usage:
Sex::put('current_user', $user);
$user = Sex::get('current_user');
// Custom namespaced instance:
$customSex = new Sex('custom_namespace');
$customSex->ensure()->put('foo', 'bar');
// Clean shutdown
Sex::destroy();
Tags
Table of Contents
Properties
Methods
- __call() : mixed
- Magic method to proxy dynamic instance method calls to internal protected implementations.
- __callStatic() : mixed
- Magic method to proxy static method calls to the global Sex instance.
- __construct() : mixed
- Initialize a Sex instance with an optional namespace key.
- clear() : self
- destroy() : void
- Destroy all PHP session data across all namespaces.
- ensure() : self
- get() : mixed
- global() : self
- Retrieve the global Sex instance.
- initGlobal() : void
- Initialize the global Sex instance for application-wide access.
- isActive() : bool
- Check if a session is currently active.
- put() : self
- require() : self
- _clear() : static
- Clear all namespaced session data for this instance.
- _ensure() : static
- Ensure a session is started.
- _get() : mixed
- Retrieve a value from the session under the instance namespace.
- _put() : static
- Store a value in the session under the instance namespace.
- _require() : static
- Require that a session is active.
Properties
$instance
private
static self|null
$instance
= \null
$key
private
string
$key
$prefixes
private
static array<string|int, mixed>
$prefixes
= []
Methods
__call()
Magic method to proxy dynamic instance method calls to internal protected implementations.
public
__call(string $name, array<string|int, mixed> $arguments) : mixed
Parameters
- $name : string
-
Method name without leading underscore.
- $arguments : array<string|int, mixed>
-
Method arguments.
Tags
__callStatic()
Magic method to proxy static method calls to the global Sex instance.
public
static __callStatic(string $name, array<string|int, mixed> $arguments) : mixed
Parameters
- $name : string
-
Method name without leading underscore.
- $arguments : array<string|int, mixed>
-
Method arguments.
Tags
__construct()
Initialize a Sex instance with an optional namespace key.
public
__construct(string $key) : mixed
Does NOT start the session immediately (lazy initialization).
Parameters
- $key : string
-
Namespace key for this instance.
Tags
clear()
public
clear() : self
Clear all namespaced session data for this instance.
Return values
selfdestroy()
Destroy all PHP session data across all namespaces.
public
static destroy() : void
Tags
ensure()
public
ensure() : self
Ensure a session is started on this instance.
Return values
selfget()
public
get(string $key) : mixed
Retrieve a value from this instance's namespace.
Parameters
- $key : string
global()
Retrieve the global Sex instance.
public
static global() : self
Tags
Return values
selfinitGlobal()
Initialize the global Sex instance for application-wide access.
public
static initGlobal() : void
Call once during application bootstrap (e.g., in index.php).
Tags
isActive()
Check if a session is currently active.
public
static isActive() : bool
Tags
Return values
bool —True if session is active (PHP_SESSION_ACTIVE), false otherwise.
put()
public
put(string $key, mixed $value) : self
Store a value in this instance's namespace.
Parameters
- $key : string
- $value : mixed
Return values
selfrequire()
public
require() : self
Enforce that an active session is running on this instance.
Return values
self_clear()
Clear all namespaced session data for this instance.
protected
_clear() : static
Return values
static —Returns $this for chaining.
_ensure()
Ensure a session is started.
protected
_ensure() : static
Return values
static —Returns $this for fluent chaining.
_get()
Retrieve a value from the session under the instance namespace.
protected
_get(string $key) : mixed
Parameters
- $key : string
-
The session key name (will be namespaced).
Return values
mixed —The stored value, or null if not found.
_put()
Store a value in the session under the instance namespace.
protected
_put(string $key, mixed $value) : static
Parameters
- $key : string
-
The key name (will be namespaced).
- $value : mixed
-
The value to store.
Return values
static —Returns $this for chaining.
_require()
Require that a session is active.
protected
_require() : static
Tags
Return values
static —Returns $this for fluent chaining.