Core PHPPHP

CoreEnv PHP – A Lightweight Laravel-Style .env Loader for Core PHP 8+

CoreEnv PHP is a minimal, Laravel-style environment variable loader and manager built specifically for Core PHP 8+ applications. It offers seamless `.env` file handling, typed accessors, and validation — all without relying on any frameworks. This makes it perfect for lightweight projects, micro-apps, or legacy systems where you still want the comfort of Laravel-like `.env` support.

Key Features

  • Multi-layer .env Loading — Automatically loads .env, .env.{APP_ENV}, and .env.local files with correct override order.
  • Global Variable Population — Populates putenv(), $_ENV, and $_SERVER safely.
  • Typed Getters — Retrieve environment values as string, int, float, bool, or array types with defaults.
  • Required Variable Validation — Enforce presence of critical keys via requireVars().
  • Zero-dependency — No external packages or frameworks required.
  • Safe Defaults — Existing environment variables are not overwritten.

Installation

composer require sachin-sanchania/coreenv-php

That’s all you need to bring environment management to your Core PHP project — instantly.

Quick Start Example


require 'vendor/autoload.php';
use CoreEnv\Env;

// Initialize the environment loader (path to folder containing .env files)
$env = Env::getInstance(__DIR__ . '/');

// Access environment variables
echo $env->getString('APP_NAME', 'MyApp');
echo $env->getBool('APP_DEBUG') ? 'debug' : 'prod';

This initializes CoreEnv, loads your .env hierarchy, and lets you retrieve values in a strongly-typed way.

.env Priority and Merging Order

CoreEnv reads and merges environment files in the following order, where later files override earlier ones:

  • .env — Base configuration (always loaded first)
  • .env.{APP_ENV} — Environment-specific overrides (e.g. .env.production)
  • .env.local — Local developer overrides (highest priority)

CoreEnv PHP API


Env::getInstance(string $path = __DIR__ . '/../'); // Returns singleton instance

// Getters
$env->get('APP_ENV', 'production');
$env->getString('APP_NAME', 'MyApp');
$env->getInt('PORT', 8080);
$env->getFloat('THRESHOLD', 0.5);
$env->getBool('APP_DEBUG', false);
$env->getArray('ALLOWED_HOSTS', []);

// Validate required keys
$env->requireVars(['APP_KEY', 'DB_HOST']);

Example Project

You can explore the example/ folder in the GitHub repo to see a ready-to-run demo:

  • .env — Default environment file
  • .env.local — Local overrides
  • .env.prod — Production configuration
  • index.php — Usage demonstration

Security & Best Practices

  • Never commit real .env files with secrets to version control — only commit .env.example.
  • Keep production environment files outside your webroot with proper permissions.
  • In CI/CD or containers, use server-level environment variables instead of files.
  • Validate required variables using requireVars() during bootstrap.

Why CoreEnv PHP?

If you’ve ever missed Laravel’s environment handling while working on a plain PHP project, CoreEnv PHP bridges that gap. It provides all the conveniences of Laravel’s env() without needing the full framework — ideal for microservices, internal scripts, or legacy apps being modernized step by step.

Conclusion

CoreEnv PHP offers a clean, reliable way to manage environment variables in any PHP 8+ project. With typed accessors, layered loading, and validation built-in, it’s an elegant alternative to bulky configuration systems.

Give it a try in your next Core PHP project, and enjoy Laravel-like configuration management — without the overhead.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button