- How do I install oscarotero/env with Composer?
- Install it with composer require oscarotero/env. It has no dependencies and is PSR-friendly, so it works in any Composer-based PHP 8.x project. After install, call the Env class accessors directly; no Laravel service provider is required.
- Does oscarotero/env work with Laravel?
- Yes, it is framework-agnostic and can run inside Laravel. However, Laravel already bundles vlucas/phpdotenv and provides the env() helper, so the package is largely redundant in a standard app. If you use it, confine calls to bootstrap and config files, never runtime application code, because config:cache bakes values.
- What is the difference between oscarotero/env and vlucas/phpdotenv?
- phpdotenv is primarily a .env loader, while oscarotero/env focuses on deterministic typed reads such as bool, int, float, array, and oneOf. oscarotero/env also offers a lightweight Env::init() loader, but its differentiator is typed access. In Laravel the loading problem is already solved, so evaluate it only for the typed-read gap.
- How do I load a .env file with Env::init()?
- Call Env::init() early in your bootstrap process to load a .env file into the environment. The library can also read values from $_ENV, $_SERVER, or getenv(), so loading is optional. Running it alongside Laravel's Dotenv bootstrapper creates precedence ambiguity across those sources, so define who owns the resolution order.
- How does automatic type casting work in oscarotero/env?
- It automatically casts strings such as 'true', 'false', 'null', and numeric values into booleans, null, integers, or floats. The coercion is opinionated, so values like '0', an empty string, and 'false' deserve explicit tests. Silent mis-coercion is the most likely production defect.
- Can I set default values for environment variables?
- Yes. Env::get($key, $default) returns a default when the variable is missing. Use the typed helpers or a custom converter when you need consistent bool, int, float, or array results. The default should match the expected type to avoid surprises.
- How do I create custom conversion functions for env values?
- The package supports custom conversion functions for special cases. Use them for domain-specific parsing, enums, or structured values that the built-in casts do not cover. Keep conversions deterministic and add tests for edge cases such as empty strings and numeric-looking strings.
- Should I use oscarotero/env in Laravel config files?
- It can be used there, but Laravel's env() helper already reads through phpdotenv. Because config:cache bakes configuration values, any environment read must happen in config files or bootstrap, not at runtime. If you adopt it, wrap it behind an internal Env facade so call sites can be swapped back to native env() without churn.
- Does oscarotero/env work with Laravel Octane or Swoole?
- In long-running workers such as Octane or Swoole, environment variables are typically read once at boot and then frozen. oscarotero/env does not change that behavior or remove Laravel's config:cache constraints. Test resolution order and putenv behavior against your hosting setup before rollout.
- Is oscarotero/env safe for production and who maintains it?
- Technically the risk is low: it is a zero-dependency, single-purpose utility. The ownership and supply-chain risk is moderate-to-high because the project has roughly ninety stars, a single maintainer, a small community, and no formal security posture. Pin the exact version, add it to dependency-policy review, and consider vendoring or forking if adoption grows.