php-standard-library/env
Tiny PHP utility for reading environment variables with sensible defaults and type casting. Helps centralize access to config via env(), supports required keys, fallback values, and safe handling when variables are missing or empty.
env() helper but fills gaps in type safety and framework-agnostic consistency. Ideal for:
getenv() with a maintainable API.envBool('FEATURE_FLAG', false)) to prevent runtime errors.config() system by acting as a low-level abstraction for non-cached variables (e.g., runtime overrides, CLI tools).env() + phpdotenv for advanced features (e.g., validation, encryption).getenv()/$_ENV in non-Laravel contexts (e.g., Artisan commands, queues). For Laravel web layers, it’s best used as a utility layer alongside config().env() wrappers or custom config loaders.config() caches .env values; this package doesn’t, which could lead to performance tradeoffs in high-traffic apps.envBool('KEY') may return false for "0", "false", or null, leading to unexpected behavior. Requires explicit validation rules.envString() + manual casting.config('key') to envString('KEY') due to caching differences.getenv() calls in frameworks like Symfony or plain PHP may need selective replacement.$_ENV or using putenv()).setEnvVarsForTest()).getenv() usage, or only in specific layers (e.g., CLI tools)?"off" for booleans)?null defaults, empty strings) be standardized?getenv() calls be deprecated first (via linters) before replacement?env() for consistency?config() but avoids caching overhead. Example:
// Instead of:
if (config('features.new_ui')) { ... }
// Use:
if (envBool('FEATURE_NEW_UI', false)) { ... }
getenv() anti-patterns.| Phase | Action | Tools/Examples |
|---|---|---|
| Audit | Identify getenv()/$_ENV usage via static analysis. |
git grep -r 'getenv|$_ENV' -- '!vendor/' |
| Pilot (CLI/Jobs) | Replace getenv() in Artisan commands, queues, and cron jobs. |
Before: getenv('QUEUE_DRIVER') After: envString('QUEUE_DRIVER') |
| Shared Libraries | Adopt in framework-agnostic code (e.g., shared kernels, utilities). | envInt('MAX_RETRIES', 3) |
| Laravel Bridge | Create a facade to unify env() and the package: |
```php |
// app/Helpers/EnvHelper.php
function env($key, $default = null) {
return app()->runningInConsole()
? \php_stdlib\env\env($key, $default)
: config($key, $default);
}
``` |
| Web Layer (Optional)| Replace config() for non-cached variables (e.g., runtime flags). | envBool('MAINTENANCE_MODE') instead of config('maintenance') |
| Deprecation | Add PHPStan rules to flag getenv() usage. | Custom rule: NoGetenvUsage |
config(); no framework modifications..env file scanning.config() for cached ones..env loading (e.g., vlucas/phpdotenv).envInt() accepts "abc").getenv() in non-web code first.getenv() via linters and IDE warnings.getenv() calls with defaults.SNAKE_CASE conventions across teams.composer.json, requiring version updates.env() may require package updates.envInt() fails on "abc").getenv() calls if not wrapped.env* syntax; minimal learning curve.config() caching.env() vs. envString().getenv(); no significant impact on memory/CPU.config(), this package doesn’t cache by default, which is good for runtime flexibility but may require manual caching in high-frequency loops.How can I help you explore Laravel packages today?