PHP Luminova: Request Server Parameters
With the Server class, managing server-related variables becomes easier and more straightforward, offering convenient methods for retrieving, manipulating, and validating server data.
The Server class acts as a structured wrapper around PHP's $_SERVER superglobal, providing a more organized way to interact with server-related data. By initializing the Server class with an array representation of $_SERVER, it allows for easier manipulation and access to server variables, enhancing the efficiency and clarity of server-side operations.
Class Definition
- Class namespace:
Luminova\Http\Server - This class implements: Luminova\Interface\LazyObjectInterface, \Countable
Example Usage
The Server object is accessible via the request class as a public property: $request->server.
Get a Value for a Specific Key:
$value = $server->get('SERVER_NAME');Set a New Value for a Key:
$server->set('QUERY_STRING', 'Default Query String');Remove a Key:
$server->remove('SERVER_PORT');Search for a Value:
$key = $server->search('localhost');Check if a Key Exists:
if ($server->has('SERVER_SIGNATURE')) {
// Key exists
}Count the Number of Elements:
$count = $server->count();Properties
variables
An array holding server variables.
/**
* @var array<string, mixed>
*/
protected array $variables = [];Methods
constructor
Initializes the Server class with an array of server variables.
public function __construct(array $variables = []);Parameters:
| Parameter | Type | Description |
|---|---|---|
$variables | array<array-key, mixed> | An Associative array of server variables to initialize with. |
get
Retrieves server variables.
/**
* @return mixed|array<array-key, mixed>|string|null
*/
public function get(?string $name = null, mixed $default = null): mixed;Parameters:
| Parameter | Type | Description |
|---|---|---|
$name | string|null | The name of the server variable to retrieve. |
$default | mixed | The default value if the variable is not found. |
Return:
mixed - Return the value of the specified server variable, or all variables if $name is null.
set
Sets a server variable.
public function set(string $key, mixed $value): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key of the server variable to set. |
$value | mixed | The value to assign to the server variable. |
Return:
self - Returns the current instance.
remove
Removes a server variable by key.
public function remove(string $key): void;Parameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key of the server variable to remove. |
search
Searches for a value by key in the server variables.
public function search(string $key, mixed $default = false): mixed;Parameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to search for. |
$default | mixed | The default value if the key is not found. |
Return:
mixed - Return the value of the found variable or false if not found.
has
Checks if a server variable exists by key.
public function has(string $key): bool;Parameters:
| Parameter | Type | Description |
|---|---|---|
$key | string | The key to check for existence. |
Return:
bool - Returns true if the key exists, false otherwise.
count
Counts the total number of server variables.
public function count(): int;Return:
int - Return the number of server variables.
fromGlobal
Get a shared instance that reads from the current $_SERVER array.
public static function fromGlobal(): staticReturn:
static - Returns the shared global server instance.
isGlobal
Check where the instance reads its variables.
public function isGlobal(): boolReturn:
bool - Returns true when the backing data comes from $_SERVER.
all
Retrieve all server variables from the instance’s active source.
public function all(): arrayReturn:
array<string,mixed> - Returns the available server variables keyed by name.
many
Retrieve several server variables in one call.
public function many(array $names, bool $skipMissing = true): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$names | string[] | Variable names to retrieve. |
$skipMissing | bool | Skip missing variables when true. Include them with null values when false. |
Return:
array<string,mixed> - Returns the selected values keyed by server variable name.
getHeaders
Extract HTTP headers from the instance’s server variables.
public function getHeaders(): arrayReturn:
array<string,string> - Returns normalized header names and string values.
getDefault
Build default server variables using the application environment. Includes the host, port, request timestamps, entry script, and a generated request identifier.
public static function getDefault(): arrayReturn:
array<string,string|int|float> - Returns the default variables keyed by server variable name.