envfile

.env parser, validator and config container with interpolation and typed getters.

v1.0.0Data formatsbundledimport "envfile";
Overview Installation Documentation Examples

Overview

A zero-dependency .env file parser, validator, and typed configuration container for HolyPHP.

Supports comments, export prefixes, single/double-quoted values, escape sequences, ${VAR} interpolation with defaults, and strict typed getters with clear error messages.

bundled — works offline zero dependencies dotenv config

Quick start

import "dotenv";

$content = "
# Application settings
APP_NAME=\"HolyPHP Service\"
HOST=localhost
PORT=8080
BASE_URL=http://\${HOST}:\${PORT}
DEBUG=true
";

$env = DotEnv::parse($content);
$env->requireAll(["APP_NAME", "BASE_URL"]);

println($env->require("APP_NAME"));       // HolyPHP Service
println($env->get("BASE_URL"));           // http://localhost:8080
println($env->getInt("PORT", 3000));      // 8080
println($env->getBool("DEBUG", false));   // true

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install envfile

This adds the library to hphp.yaml, installs it into .hphp/packages/envfile/ and pins the exact version in hphp.lock:

require:
  envfile: "^1.0"

Use it

import "envfile";

envfile is bundled with the compiler, so import "envfile"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install envfile          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install envfile
Packageenvfile
Version1.0.0 (follows the compiler)
Entry fileenvfile.hphp
Sourcepackages/lib/envfile ↗

Documentation

Syntax Supported

API Reference

Functions

Class DotEnv

Error Handling

All parse and validation errors throw Exception with a dotenv: prefix and actionable details (including line numbers for parse errors):

Examples

01Quick start

Bundled with the compiler — import "envfile"; works offline in any file, or add it to a project with hphp install envfile.

import "dotenv";

$content = "
# Application settings
APP_NAME=\"HolyPHP Service\"
HOST=localhost
PORT=8080
BASE_URL=http://\${HOST}:\${PORT}
DEBUG=true
";

$env = DotEnv::parse($content);
$env->requireAll(["APP_NAME", "BASE_URL"]);

println($env->require("APP_NAME"));       // HolyPHP Service
println($env->get("BASE_URL"));           // http://localhost:8080
println($env->getInt("PORT", 3000));      // 8080
println($env->getBool("DEBUG", false));   // true
Try it
hphp install envfile
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_envfile.hphp.