phpEZ v0.4.1

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
see
Model::toSex()
Model::fromSex()

--- STATIC GLOBAL API (Sex::method) ---

subpackage

sex

Table of Contents

Properties

$instance  : self|null
$key  : string
$prefixes  : array<string|int, mixed>

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
throws
BadMethodCallException

If target internal method does not exist.

__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
throws
LogicException

If global instance is uninitialized.

BadMethodCallException

If target internal method does not exist.

__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
throws
LogicException

If sessions are disabled or if key is already registered.

clear()

public clear() : self

Clear all namespaced session data for this instance.

Return values
self

destroy()

Destroy all PHP session data across all namespaces.

public static destroy() : void
Tags
throws
RuntimeException

If session_destroy() fails.

ensure()

public ensure() : self

Ensure a session is started on this instance.

Return values
self

get()

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
throws
LogicException

If Sex::initGlobal() has not been called.

Return values
self

initGlobal()

Initialize the global Sex instance for application-wide access.

public static initGlobal() : void

Call once during application bootstrap (e.g., in index.php).

Tags
throws
LogicException

If the global instance is already initialized.

put()

public put(string $key, mixed $value) : self

Store a value in this instance's namespace.

Parameters
$key : string
$value : mixed
Return values
self

require()

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
throws
LogicException

If no session is active.

Return values
static

Returns $this for fluent chaining.

On this page

Search results