darlanschmeller / php-env-loader
Lightweight .env file loader for PHP with automatic type casting
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/darlanschmeller/php-env-loader
Requires
- php: >=8.0
README
A lightweight, object-oriented PHP library to load .env files into $_ENV with automatic type casting and quote handling. Perfect for small projects, scripts, or when you want a simple alternative to vlucas/phpdotenv.
Features
- Load
.envfiles into$_ENVautomatically - Supports
exportkeyword - Inline comment handling (
# comment) - Quote string handling (
"string"or'string') - Automatic type casting:
"true"/"false"→bool- Numeric strings →
intorfloat "null"→null
- Static helper method for convenience
Installation
Clone the repository:
git clone https://github.com/DarlanSchmeller/php-env-loader.git
Include in your project:
require __DIR__ . '/src/EnvLoader.php'; use Src\EnvLoader;
Usage
Basic Usage
$env = new EnvLoader(); // Defaults to '../.env' $variables = $env->load(); var_dump($variables); // Loaded env variables var_dump($_ENV); // Also accessible globally
Static Convenience
If you just want to load a .env without instantiating the class:
$variables = EnvLoader::loadFrom(__DIR__ . '/.env');
Sample .env
DB_HOST=127.0.0.1 DB_PORT=3306 DB_USER=root DB_PASSWORD="supersecret" APP_DEBUG=true TIMEOUT=5.5
After loading
$_ENV['DB_PORT']; // int(3306) $_ENV['DB_PASSWORD']; // string("supersecret") $_ENV['APP_DEBUG']; // bool(true) $_ENV['TIMEOUT']; // float(5.5)
Note
- Inline comments are supported outside quotes, e.g.,
DB_HOST=127.0.0.1 # main database host - Quotes around values are stripped automatically
- Empty strings and "null" values are converted to null