Neuron-PHP

Env
in package

Environment variable manager and .env file loader for the Neuron framework.

This singleton class provides secure and convenient access to environment variables and .env file configuration. It automatically loads environment variables from .env files and provides a clean API for accessing configuration values throughout the application with proper error handling.

Key features:

  • Automatic .env file detection and loading
  • Singleton pattern for consistent environment state
  • Comment support in .env files (lines starting with #)
  • Secure environment variable access with null safety
  • Custom .env file path support
  • Environment variable validation and type conversion

The class automatically looks for .env files in the document root unless a custom path is specified, making it ideal for configuration management across different deployment environments.

Tags
example
// .env file contents:
// DB_HOST=localhost
// DB_PORT=3306
// DEBUG=true
// # This is a comment
// API_KEY=secret_key_here

// Load and access environment variables
$env = Env::getInstance();

$dbHost = $env->get('DB_HOST');      // 'localhost'
$dbPort = $env->get('DB_PORT');      // '3306'
$debug = $env->get('DEBUG');         // 'true'
$missing = $env->get('MISSING');     // null

// Load custom .env file
$env = Env::getInstance('/path/to/custom/.env');

// Set environment variables programmatically
$env->put('RUNTIME_CONFIG=dynamic_value');

Table of Contents

Properties

$fileName  : string|null
$fs  : IFileSystem
$instance  : Env|null

Methods

get()  : array<string|int, mixed>|false|string
getInstance()  : Env|null
loadEnvFile()  : void
put()  : bool
reset()  : void
__construct()  : mixed
Env constructor.

Properties

$fileName

private string|null $fileName

$instance

private static Env|null $instance = null

Methods

get()

public get(mixed $key) : array<string|int, mixed>|false|string
Parameters
$key : mixed
Return values
array<string|int, mixed>|false|string

getInstance()

public static getInstance([string|null $envFile = null ][, IFileSystem|null $fs = null ]) : Env|null
Parameters
$envFile : string|null = null
$fs : IFileSystem|null = null

File system implementation (null = use real file system)

Return values
Env|null

loadEnvFile()

public loadEnvFile() : void

put()

public put(mixed $config) : bool
Parameters
$config : mixed
Return values
bool

reset()

public reset() : void

__construct()

Env constructor.

private __construct([string|null $fileName = null ][, IFileSystem|null $fs = null ]) : mixed
Parameters
$fileName : string|null = null
$fs : IFileSystem|null = null

File system implementation (null = use real file system)


        
On this page

Search results