Alpha ZealPHP is early-stage and under active development. APIs may change between minor versions until v1.0. Feedback and bug reports welcome on GitHub.
API Index — Namespaces, Packages, Reports, Indices

RequestContext
in package

Per-request state container. Lives on Coroutine::getContext() in coroutine mode (recommended default) so each request gets isolated state freed automatically when the coroutine ends. In legacy superglobals mode it's a process-wide singleton bridging declared properties to PHP's $_GET / $_POST / $_SESSION etc.

Previously named G — that name remains available via class_alias at the bottom of this file for backward compatibility. New code should reference RequestContext.

Table of Contents

Properties

$_session_started  : bool|null
$_streaming  : bool|null
$apacheContext  : ApacheContext|null
$cache_expire  : int|null
$cache_limiter  : string|null
$cookie  : array<string, mixed>
$error_exception  : Throwable|null
$error_handlers_stack  : array<int, array{0: callable, 1: int}>
$error_render_depth  : int
$error_reporting_level  : int|null
$error_status  : int|null
$exception_handlers_stack  : array<int, callable>
$files  : array<string, mixed>
$get  : array<string, mixed>
$ignore_user_abort_state  : int
$memo  : array<string, mixed>
$openswoole_request  : Request|null
$openswoole_response  : Response|null
$post  : array<string, mixed>
$request  : array<string, mixed>
$server  : array<string, scalar|null>
$session  : array<string, mixed>
$session_loaded_keys  : array<int, string>
Keys present in $g->session at session-load time. Lets zeal_session_write_close() distinguish an in-request unset() (key was loaded then removed → must be deleted from the store) from a concurrent add (key never loaded here → must be preserved through the merge). #21.
$session_module_name  : string|null
$session_params  : array<string, mixed>
$shutdown_functions  : array<int, array{0: callable, 1: array}>
$status  : int|null
$zealphp_request  : Request|null
$zealphp_response  : Response|null
$instance  : self|null

Methods

__get()  : mixed
Read by reference is only required in superglobals mode, where the proxy must hand back $GLOBALS['_SESSION'] etc. so legacy code that mutates $_SESSION['k'] = $v carries the write through. In coroutine mode (recommended default) all reads go through the typed properties declared above; returning by value avoids the autovivification footgun where &$g->nonexistent would create a property on first read.
__set()  : mixed
__set fires for undeclared properties AND for declared typed properties that have been unset() (the slot is "uninitialized" so direct access routes through __set on assignment). In superglobals mode we keep the legacy bridge to $GLOBALS[$key] so pre-coroutine code that stashed values via $g->custom = $val keeps working. In coroutine mode the typed properties are the contract; we re-initialize the declared slot (preserves PHP's type-check via direct property assignment) and reject any other write loudly so typos still surface.
forget()  : void
Discard the memoized value for $key in this request. The next once() call with the same key will recompute.
get()  : mixed
has()  : bool
True if once($key, ...) has been computed in this request.
instance()  : self
once()  : mixed
Compute once per request, cache for the rest of the request.
set()  : void
__construct()  : mixed

Properties

$error_handlers_stack

public array<int, array{0: callable, 1: int}> $error_handlers_stack = []

stack of [callable, levels]

$error_reporting_level

public int|null $error_reporting_level = null

$exception_handlers_stack

public array<int, callable> $exception_handlers_stack = []

stack of callables

$ignore_user_abort_state

public int $ignore_user_abort_state = 0

$openswoole_request

public Request|null $openswoole_request = null

In tests, this slot may hold a mock — see tests/Unit/RestTest.php

$openswoole_response

public Response|null $openswoole_response = null

In tests, this slot may hold a mock — see tests/Unit/RestTest.php

$session_loaded_keys

Keys present in $g->session at session-load time. Lets zeal_session_write_close() distinguish an in-request unset() (key was loaded then removed → must be deleted from the store) from a concurrent add (key never loaded here → must be preserved through the merge). #21.

public array<int, string> $session_loaded_keys = []

$session_module_name

public string|null $session_module_name = null

$session_params

public array<string, mixed> $session_params = []

$shutdown_functions

public array<int, array{0: callable, 1: array}> $shutdown_functions = []

queue of [callable, args]

$zealphp_request

public Request|null $zealphp_request = null

In tests, this slot may hold a mock — see tests/Unit/RestTest.php

$zealphp_response

public Response|null $zealphp_response = null

In tests, this slot may hold a mock — see tests/Unit/RestTest.php

Methods

__get()

Read by reference is only required in superglobals mode, where the proxy must hand back $GLOBALS['_SESSION'] etc. so legacy code that mutates $_SESSION['k'] = $v carries the write through. In coroutine mode (recommended default) all reads go through the typed properties declared above; returning by value avoids the autovivification footgun where &$g->nonexistent would create a property on first read.

public & __get(string $key) : mixed
Parameters
$key : string

__set()

__set fires for undeclared properties AND for declared typed properties that have been unset() (the slot is "uninitialized" so direct access routes through __set on assignment). In superglobals mode we keep the legacy bridge to $GLOBALS[$key] so pre-coroutine code that stashed values via $g->custom = $val keeps working. In coroutine mode the typed properties are the contract; we re-initialize the declared slot (preserves PHP's type-check via direct property assignment) and reject any other write loudly so typos still surface.

public __set(string $key, mixed $value) : mixed
Parameters
$key : string
$value : mixed

forget()

Discard the memoized value for $key in this request. The next once() call with the same key will recompute.

public static forget(string $key) : void
Parameters
$key : string

get()

public static get(string $key) : mixed
Parameters
$key : string

has()

True if once($key, ...) has been computed in this request.

public static has(string $key) : bool
Parameters
$key : string
Return values
bool

once()

Compute once per request, cache for the rest of the request.

public static once(string $key, callable $fn) : mixed

Safe alternative to static $cache = [] inside a function. Computes $fn() the first time it's called with $key in this request, caches the result on the per-coroutine RequestContext, returns the cached value on subsequent calls. The cache is freed automatically when the coroutine ends — no state survives to the next request.

Mirrors Laravel 11's once() helper. Use this anywhere you'd reach for static $foo = ... for request-scoped memoization but want to avoid leaking state into worker process memory.

$user = RequestContext::once('current_user', fn() => Auth::loadUser($id));
Parameters
$key : string
$fn : callable

set()

public static set(string $key, mixed $value) : void
Parameters
$key : string
$value : mixed
On this page