class Environment (View source)

Consolidates access and modification of PHP global variables and settings.

This class should be used sparingly, and only if information cannot be obtained from a current HTTPRequest object.

Acts as the primary store for environment variables, including those loaded from .env files. Applications should use Environment::getEnv() instead of php's getenv in order to include .env configuration, as the system's actual environment variables are treated immutably.

Properties

protected static float|null $memoryLimitMax

Set maximum limit allowed for increaseMemoryLimit

protected static int|null $timeLimitMax

Set maximum limited allowed for increaseTimeLimit

protected static array $env

Local overrides for all $_ENV var protected from cross-process operations

Methods

public static 
array
getVariables()

Extract env vars prior to modification

public static 
setVariables(array $vars)

Restore a backed up or modified list of vars to $globals

public static 
bool
increaseMemoryLimitTo(string|float|int $memoryLimit = -1)

Increase the memory limit to the given level if it's currently too low.

public static 
setMemoryLimitMax(string|float $memoryLimit)

Set the maximum allowed value for increaseMemoryLimitTo().

public static 
int
getMemoryLimitMax()

No description

public static 
bool
increaseTimeLimitTo(int $timeLimit = null)

Increase the time limit of this script. By default, the time will be unlimited.

public static 
setTimeLimitMax(int $timeLimit)

Set the maximum allowed value for increaseTimeLimitTo();

public static 
int
getTimeLimitMax()

No description

public static 
mixed
getEnv(string $name)

Get value of environment variable.

public static 
putEnv(string $string)

Set environment variable using php.ini syntax.

public static 
setEnv(string $name, string $value)

Set environment variable via $name / $value pair

public static 
bool
hasEnv(string $name)

Check if an environment variable is set

public static 
bool
isCli()

Returns true if this script is being run from the command line rather than the web server

Details

static array getVariables()

Extract env vars prior to modification

Return Value

array

List of all super globals

static setVariables(array $vars)

Restore a backed up or modified list of vars to $globals

Parameters

array $vars

static bool increaseMemoryLimitTo(string|float|int $memoryLimit = -1)

Increase the memory limit to the given level if it's currently too low.

Only increases up to the maximum defined in setMemoryLimitMax(), and defaults to the 'memory_limit' setting in the PHP configuration.

Parameters

string|float|int $memoryLimit

A memory limit string, such as "64M". If omitted, unlimited memory will be set.

Return Value

bool

true indicates a successful change, false a denied change.

static setMemoryLimitMax(string|float $memoryLimit)

Set the maximum allowed value for increaseMemoryLimitTo().

The same result can also be achieved through 'suhosin.memory_limit' if PHP is running with the Suhosin system.

Parameters

string|float $memoryLimit

Memory limit string or float value

static int getMemoryLimitMax()

No description

Return Value

int

Memory limit in bytes

static bool increaseTimeLimitTo(int $timeLimit = null)

Increase the time limit of this script. By default, the time will be unlimited.

Only works if 'safe_mode' is off in the PHP configuration. Only values up to getTimeLimitMax() are allowed.

Parameters

int $timeLimit

The time limit in seconds. If omitted, no time limit will be set.

Return Value

bool

TRUE indicates a successful change, FALSE a denied change.

static setTimeLimitMax(int $timeLimit)

Set the maximum allowed value for increaseTimeLimitTo();

Parameters

int $timeLimit

Limit in seconds

static int getTimeLimitMax()

No description

Return Value

int

Limit in seconds

static mixed getEnv(string $name)

Get value of environment variable.

If the value is false, you should check Environment::hasEnv() to see if the value is an actual environment variable value or if the variable simply hasn't been set.

Parameters

string $name

Return Value

mixed

Value of the environment variable, or false if not set

static putEnv(string $string)

Set environment variable using php.ini syntax.

Acts as a process-isolated version of putenv() Note: This will be parsed via parse_ini_string() which handles quoted values

Parameters

string $string

Setting to assign in KEY=VALUE or KEY="VALUE" syntax

static setEnv(string $name, string $value)

Set environment variable via $name / $value pair

Parameters

string $name
string $value

static bool hasEnv(string $name)

Check if an environment variable is set

Parameters

string $name

Return Value

bool

static bool isCli()

Returns true if this script is being run from the command line rather than the web server

Return Value

bool