phpEZ v0.1.2

Sex
in package

Session management helper for PHPEz.

"Sex" = "SessioN eXtensions" (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 provides app-wide access
  • Type hints: Works seamlessly with Model::toSex() / Model::fromSex()

Naming convention:

  • Root key: 'phpez_main_sex'
  • Stored keys: 'phpez_main_sex:keyname'
  • Enables safe coexistence with other session data

Usage:

// Store a value
$SEX->put('current_user', $user);

// Retrieve a value
$user = $SEX->get('current_user');

// Fluent chaining
$SEX->ensure()->put('foo', 'bar')->put('baz', 'qux');

// Clean shutdown
$SEX->destroy();
Tags
see
Model::toSex()
Model::fromSex()
subpackage

sex

Table of Contents

Properties

$key  : string
The session namespace key for this Sex instance.
$present  : bool
Whether a session was already active when this instance was created.

Methods

__construct()  : mixed
Initialize a Sex instance with optional namespace key.
destroy()  : void
Destroy the session and clear all stored data.
ensure()  : static
Ensure a session is started, starting it if necessary.
get()  : mixed
Retrieve a value from the session.
isActive()  : bool
Check if a session is currently active.
put()  : static
Store a value in the session.
require()  : static
Require an active session, throwing if not available.
getKey()  : string
Get the root session namespace key for the application.

Properties

$key

The session namespace key for this Sex instance.

private string $key

All keys stored via put() are prefixed with this: key = "$this->key:$subkey"

$present

Whether a session was already active when this instance was created.

private bool $present = \false

Used to track if we should start the session or if it was pre-existing.

Methods

__construct()

Initialize a Sex instance with optional namespace key.

public __construct([string|null $key = null ]) : mixed

If a session is already active, records that fact to avoid redundant session_start() calls. Uses provided key or the default application root key.

Does NOT start the session immediately (lazy initialization).

Parameters
$key : string|null = null

Optional namespace key for this instance. If null, uses getKey() (global namespace). Allows multiple Sex instances with different namespaces.

destroy()

Destroy the session and clear all stored data.

public destroy() : void

Ensures a session is started (if not already) and then destroys it, clearing all $_SESSION data.

Tags
example
$SEX->destroy();  // Logout: clear all session data

ensure()

Ensure a session is started, starting it if necessary.

public ensure() : static

Safely starts the session only if it's not already active. Returns $this for fluent method chaining.

Safe to call multiple times; only calls session_start() once.

Return values
static

Returns $this for chaining.

get()

Retrieve a value from the session.

public get(string $key) : mixed

Requires an active session, then retrieves the value using the namespaced key. Returns null if the key is not found.

Parameters
$key : string

The session key name to retrieve (will be namespaced).

Tags
throws
LogicException

If no session is active (via require()).

example
$user = $SEX->get('user');
$token = $SEX->get('auth_token') ?? null;
Return values
mixed

The stored value, or null if not found.

put()

Store a value in the session.

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

Ensures a session is started, then stores the value in $_SESSION with the key prefixed by the namespace.

Returns $this for fluent chaining.

Parameters
$key : string

The session key name (will be namespaced).

$value : mixed

The value to store. Can be any serializable type.

Tags
example
$SEX->put('user', $user)
    ->put('auth_token', $token)
    ->put('preferences', ['theme' => 'dark']);
Return values
static

Returns $this for chaining.

require()

Require an active session, throwing if not available.

public require() : static

Verifies that a session is currently active. Throws LogicException if a session is not available (e.g., headers already sent, session disabled).

Used before reading from the session to catch configuration problems early.

Tags
throws
LogicException

If no session is active.

Return values
static

Returns $this for chaining.

getKey()

Get the root session namespace key for the application.

protected static getKey() : string

All Sex instance keys are prefixed with this to namespace data and prevent conflicts with other session data.

Return values
string

The root session namespace 'phpez_main_sex'.

On this page

Search results