-
-
Notifications
You must be signed in to change notification settings - Fork 0
The Session Facade
InitPHP\Sessions\Session is the single entry point you interact with. It is a
facade: a thin layer that forwards calls to two small collaborators.
| Collaborator | Responsibility | Methods |
|---|---|---|
Classes\Manager |
The session lifecycle (session_*() wrappers) |
start, isStarted, getName, setName, getID, setID, regenerateId, flush, unset, destroy
|
Classes\GetterSetter |
$_SESSION access |
has, get, set, push, pull, remove, delete, all, setAssoc
|
You never construct Manager or GetterSetter directly — the facade creates and
routes to them for you.
use InitPHP\Sessions\Session;
Session::createImmutable(); // no save handler → PHP default
Session::createImmutable($adapter); // custom save handler (an Adapter)createImmutable() does two things:
- Builds a fresh
Manager(registering your adapter as the save handler) and a freshGetterSetter. - Returns a
Sessioninstance, so you can chain straight intostart():
Session::createImmutable($adapter)
->start();Call it once per request, before you touch any other method. Calling it again rebuilds the collaborators (handy in tests).
Every method is exposed both statically and on the instance. Both are routed to the same underlying objects, so they operate on the same session:
$session = Session::createImmutable();
$session->start(); // instance
Session::set('user', 'ada'); // static
echo $session->get('user'); // 'ada' — same stateInternally this is powered by __callStatic() (static calls) and __call()
(instance calls), both delegating through a single router. The documented,
canonical style across this wiki is static (Session::set(...)), but use
whichever reads better.
The instance exposes the two collaborators as read-only properties, should you ever want to pass one around:
$session = Session::createImmutable();
$session->manager; // InitPHP\Sessions\Classes\Manager
$session->session; // InitPHP\Sessions\Classes\GetterSetterThe router knows exactly which methods belong to which collaborator. Anything
else raises a SessionException:
Session::createImmutable();
Session::doesNotExist();
// SessionException: "doesNotExist" method is not found.| Call | Returns |
|---|---|
Session::createImmutable() |
Session (for chaining) |
Session::set(...) / push excluded |
GetterSetter (chain more value calls) |
Session::push(...) |
the stored value |
Session::setName(...) |
Manager (chain more lifecycle calls) |
| everything else | a scalar/array/bool as documented in the API Reference |
Because set() returns the GetterSetter, value writes chain naturally:
Session::set('a', 1)->set('b', 2)->setAssoc(['c' => 3]);initphp/sessions · MIT License · part of the InitPHP family
Source · Issues · Discussions · Packagist · Contributing · Security Policy
Getting Started
Core Usage
Adapters
- Adapters Overview
- File Adapter
- Redis Adapter
- PDO Adapter
- Cookie Adapter
- Memcache / Memcached Adapter
- MongoDB Adapter
- Custom Adapters
Reference
Practical Guides
Migration & Help