PHP Luminova: Runtime Shared Memory
Store and retrieve temporary values within one PHP runtime.
Use Luminova\Runtime to share temporary keyed values among code running in the same PHP process. The storage is in memory and ends with the PHP runtime. For a long-running CLI process, values remain available until they are removed or the process exits.
Usages
use Luminova\Runtime;
// Store a value
Runtime::set('theme', 'dark');
// Get a value
$theme = Runtime::get('theme') ?? 'light';
// Check if it exists
if (Runtime::has('theme')) {
// ...
}
// Remove one value
Runtime::remove('theme');
// Clear everything
Runtime::clear();
// Count how many keys are stored
$count = Runtime::count();Reuse a Value Within the Same Runtime
Use shared() to initialize a value in one part of the application and read it in another. The named default argument supplies a fallback on reads.
use Luminova\Runtime;
use function Luminova\Funcs\shared;
shared('catalog.locale', 'en');
$locale = shared('catalog.locale', default: 'en');
Runtime::remove('catalog.locale');Class Definition
- Full namespace:
Luminova\Runtime - This class is final.
See Also:
Luminova\Funcs\shared()- Global helper for quick access.
Shared Memory Methods
set
Set a shared value.
Stores the value. If both the new and existing values are arrays, matching keys are replaced.
public static function set(string $key, mixed $value): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to store the value under. |
$value | mixed | The value to store. |
Return:
mixed - Returns the stored value.
get
Get a shared value.
Retrieves a value stored under the specified key.
public static function get(string $key): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to retrieve. |
Return:
mixed - Returns the stored value or null if not found.
Note:This method returns
nullif the key does not exist.
has
Check if a key exists in the shared storage.
public static function has(string $key): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to check. |
Return:
bool - Return true if the key exists, false otherwise.
remove
Remove a key from the shared storage.
public static function remove(string $key, bool $userDefined = true): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Key to remove. |
$userDefined | bool | Protect framework-reserved keys (default true). |
clear
Clear application-defined keys. Framework-reserved entries remain.
public static function clear(): voidcount
Count all stored keys, including framework entries.
public static function count(): intReturn:
int - Return the number of stored keys.
add
Assign one entry inside a shared array. The method initializes an absent or null storage key to an empty array. Use this method with an array-valued key.
public static function add(string $key, string|int $index, mixed $value): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | Shared storage key. |
$index | string\|int | Index to set. |
$value | mixed | Value to store. |
Return:
mixed - Returns the stored value.