-
Notifications
You must be signed in to change notification settings - Fork 2
Env
The Env class provides a tiny and predictable way to load environment variables from a .env file into $_ENV, with:
- sensible defaults (fallback values);
- automatic type casting for booleans and numbers;
- a single accessor method to read variables consistently.
This is useful when you want to configure the same Pair application across multiple environments (local, staging, production) without hardcoding values in PHP files.
The
.envfile is expected to be placed in the project root (APPLICATION_PATH/.env).
The .env file is a plain text file where each non-empty line is:
KEY=value
You can also add comments (lines starting with #), which will be ignored.
Example:
# Application
APP_NAME=My Pair App
APP_ENV=development
APP_DEBUG=true
# Pair / security
PAIR_SINGLE_SESSION=false
# OAuth2
OAUTH2_TOKEN_LIFETIME=3600The absolute path of the .env file.
Env::FILE; // APPLICATION_PATH . '/.env'An associative array containing fallback values used when:
- the
.envfile is missing, or - a requested key is not present in
$_ENV.
Current defaults:
-
APP_NAME(string) -
APP_VERSION(string) -
APP_ENV(string) -
APP_DEBUG(bool) -
DB_UTF8(bool) -
OAUTH2_TOKEN_LIFETIME(int, fromOAuth2Token::LIFETIME) -
PAIR_SINGLE_SESSION(bool) -
PAIR_AUDIT_ALL(bool) -
PAIR_AUTH_BY_EMAIL(bool) -
UTC_DATE(bool)
Defaults are intentionally conservative: they make the app run even when
.envis missing.
Call Env::load() early in the bootstrap, typically in index.php or right after the Application startup, so that $_ENV is ready before other components read configuration.
Example (simplified index.php idea):
<?php
use Pair\Core\Application;
use Pair\Core\Env;
// start the application
$app = Application::getInstance();
// load .env variables into $_ENV
Env::load();
// now you can safely read Env values
if (Env::get('APP_DEBUG')) {
ini_set('display_errors', '1');
}Returns true if the .env file exists at Env::FILE.
Typical usage:
use Pair\Core\Env;
if (!Env::fileExists()) {
// show a warning, or rely on defaults
}Returns the value for a given key, following this priority:
-
$_ENV[$key](if already loaded / set) -
Env::DEFAULTS[$key](fallback) -
null(if not found anywhere)
This allows consistent reads even when .env is not present.
Examples:
use Pair\Core\Env;
// read a boolean
$debug = Env::get('APP_DEBUG'); // true/false (or default)
// read a string
$appName = Env::get('APP_NAME'); // "Pair Application" by default
// unknown keys return null
$foo = Env::get('SOME_UNKNOWN_KEY'); // null
Env::get()does not throw if a key is missing; it is designed for predictable fallbacks.
Loads the .env file into $_ENV.
Behavior:
- If
.envdoes not exist:- loads every entry from
Env::DEFAULTSinto$_ENV - returns immediately
- loads every entry from
- If
.envexists:- reads the file line by line
- ignores:
- empty lines
- lines starting with
# - lines without an
=symbol
- splits each valid line into
KEYandvalue - trims both key and value
- casts types:
-
"true"/"false"(case-insensitive) ->true/false - numeric integers ->
(int) - numeric floats ->
(float) - everything else stays as string
-
- assigns the final value into
$_ENV[$key]
Given this .env:
APP_DEBUG=true
OAUTH2_TOKEN_LIFETIME=3600
DISCOUNT=12.5
APP_NAME=My AppAfter Env::load():
use Pair\Core\Env;
Env::load();
var_dump(Env::get('APP_DEBUG')); // bool(true)
var_dump(Env::get('OAUTH2_TOKEN_LIFETIME')); // int(3600)
var_dump(Env::get('DISCOUNT')); // float(12.5)
var_dump(Env::get('APP_NAME')); // string("My App")If the .env file does not exist, Pair will still work using defaults:
use Pair\Core\Env;
Env::load();
echo Env::get('APP_ENV'); // "production"
echo Env::get('APP_DEBUG'); // false- Keep secrets (API keys, tokens, DSNs) in
.envand out of Git. - Call
Env::load()once during bootstrap (do not reload repeatedly). - Use
Env::get()instead of reading$_ENVdirectly, so defaults always apply. - Prefer boolean and numeric values in
.envwhen you want type-safe reads.
- The
.envloader is intentionally minimal: it focuses on predictable parsing and safe defaults. - Lines without
=are ignored, as well as comments starting with#.
See also: Configuration-file, Application, index.php, Logger.