uniqueKey.php
Table of Contents
Functions
- uniqueKey() : string
- Generates a unique, deterministic key from context and parameters.
Functions
uniqueKey()
Generates a unique, deterministic key from context and parameters.
uniqueKey(string|null $context[, array<string|int, mixed>|null $binds = null ][, string $prefix = '' ][, string $separator = ':' ][, bool $hash = true ]) : string
This function produces a stable key suitable for caching or indexing. The key is guaranteed to be identical for the same input values regardless of parameter order. Values are serialized consistently, and the final key is optionally hashed using SHA-256 to produce a fixed-length string.
Key generation process:
- Start with the prefix (e.g., cache namespace)
- Add the context identifier if provided
- Sort bind parameters alphabetically by key
- Serialize each bind value according to its type:
- null → 'null'
- boolean → 'true' or 'false'
- array → JSON encoded
- object → serialized, unless a Closure → 'closure'
- other → cast to string
- Join all parts with the separator
- Normalize the resulting string using Normalizer::FORM_C
- Optionally hash the string with SHA-256
Type handling for bind values:
- null → 'null'
- boolean → 'true' or 'false'
- array → JSON encoded
- object → JSON encoded
- other → string cast
Parameters
- $context : string|null
-
Context identifier (e.g., operation or query name) used to differentiate cache entries.
- $binds : array<string|int, mixed>|null = null
-
Associative array of parameters to include in the key. Keys are sorted alphabetically to ensure consistency. Values can be scalars, arrays, objects, or closures. Example: ['provider' => 'P123', 'date' => '2025-01-15']
- $prefix : string = ''
-
Optional prefix or namespace for the key (default: '').
- $separator : string = ':'
-
Character used to join key parts (default: ':').
- $hash : bool = true
-
Whether to hash the final key using SHA-256 (default: true). If false, returns the normalized, joined string.
Tags
Return values
string —SHA-256 hash (64 hexadecimal characters) or normalized key string if $hash is false.