Luminova Framework

PHP Luminova: Request Server Parameters

Last updated: 2026-11-28 20:57:12

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


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:

ParameterTypeDescription
$variablesarray<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:

ParameterTypeDescription
$namestring|nullThe name of the server variable to retrieve.
$defaultmixedThe 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): self

Parameters:

ParameterTypeDescription
$keystringThe key of the server variable to set.
$valuemixedThe 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:

ParameterTypeDescription
$keystringThe key of the server variable to remove.

Searches for a value by key in the server variables.

public function search(string $key, mixed $default = false): mixed;

Parameters:

ParameterTypeDescription
$keystringThe key to search for.
$defaultmixedThe 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:

ParameterTypeDescription
$keystringThe 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(): static

Return:

static - Returns the shared global server instance.


isGlobal

Check where the instance reads its variables.

public function isGlobal(): bool

Return:

bool - Returns true when the backing data comes from $_SERVER.


all

Retrieve all server variables from the instance’s active source.

public function all(): array

Return:

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): array

Parameters:

ParameterTypeDescription
$namesstring[]Variable names to retrieve.
$skipMissingboolSkip 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(): array

Return:

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(): array

Return:

array<string,string|int|float> - Returns the default variables keyed by server variable name.