Luminova Framework

PHP Luminova: Management of Request Server Variables

Last updated: 2024-08-29 19:09:08

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 namespace: \Luminova\Http\Server

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.

protected array<string,mixed> $variables = [];

Methods

constructor

Initializes the Server class with an array of server variables.

public __construct(array $variables): mixed;

Parameters:

ParameterTypeDescription
$variablesarrayAn Associative array of server variables to initialize with.

get

Retrieves server variables.

public get(string|null $name = null, mixed $default = null): mixed|array|string|null;

Parameters:

ParameterTypeDescription
$namestring|nullThe name of the server variable to retrieve.
$defaultmixedThe default value if the variable is not found.

Return Value:

mixed|array|string|null - Return the value of the specified server variable, or all variables if $name is null.


set

Sets a server variable.

public set(string $key, mixed $value): void;

Parameters:

ParameterTypeDescription
$keystringThe key of the server variable to set.
$valuemixedThe value to assign to the server variable.

remove

Removes a server variable by key.

public 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 search(string $key, mixed $default = false): mixed;

Parameters:

ParameterTypeDescription
$keystringThe key to search for.
$defaultmixedThe default value if the key is not found.

Return Value:

mixed - Return the value of the found variable or false if not found.


has

Checks if a server variable exists by key.

public has(string $key): bool;

Parameters:

ParameterTypeDescription
$keystringThe key to check for existence.

Return Value:

bool - Returns true if the key exists, false otherwise.


count

Counts the total number of server variables.

public count(): int;

Return Value:

int - Return the number of server variables.