PHPackages                             didslm/env - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. didslm/env

ActiveLibrary

didslm/env
==========

A library which helps you load your environment variables into php classes.

v1.1.0(6mo ago)8400[1 issues](https://github.com/didslm/php-env/issues)MITPHPPHP ^8.0CI passing

Since Feb 1Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/didslm/php-env)[ Packagist](https://packagist.org/packages/didslm/env)[ RSS](/packages/didslm-env/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (1)Versions (8)Used By (0)

PHP Environment Variable Loader
===============================

[](#php-environment-variable-loader)

The PHP Environment Variable Loader is a library that simplifies the process of loading environment variables into PHP classes. It provides a convenient and efficient way to read and store environment variables in your PHP application. By using this library, you can easily manage your application's configuration variables, such as API keys, database credentials, and other settings, without hardcoding them into your codebase.

Installation
------------

[](#installation)

To install the library, you can use Composer. If you don't have Composer installed, please follow the instructions on the official website.

Run the following command to install the PHP Environment Variable Loader library:

```
composer require didslm/env
```

Usage
-----

[](#usage)

To use the library in your PHP code, you need to instantiate the `Didslm\Env` class and call the `populate` method:

```
class Config
{
    #[EnvObj]
    public DbConfig $mysqlConfig;
}

class DbConfig
{
    #[EnvName('DB_HOST')]
    public string $dbHost;
    #[EnvName('DB_USER')]
    public string $username;
    #[EnvName('DB_PASSWORD')]
    public string $password;
    #[EnvName('DB_DATABASE')]
    public string $database;

}

$config = new DbConfig();
//The next line will populate the class properties with the env variables if they exist.
Env::populate($config);

$allConfig = new Config();
Env::populate($allConfig);
```

### Advanced Usage

[](#advanced-usage)

- **Default values**
    Provide a fallback that is used when the environment variable (and `.env`) do not provide a value.

    ```
    class Defaults
    {
        #[EnvName('APP_REGION', default: 'eu-west-1')]
        public string $region;
    }
    ```
- **Strong typing**
    Scalar properties are automatically converted to the correct PHP type (`int`, `float`, `bool`, `string`). For booleans the loader understands `1/0`, `true/false`, `on/off`, and `yes/no`.
- **Nested configuration objects**
    Use `EnvObj` to hydrate nested objects. Optionally provide the class name if it differs from the property type.

    ```
    class Services
    {
        #[EnvObj(ServiceConfig::class)]
        public ServiceConfig $billing;
    }
    ```
- **Custom `.env` location**
    `Env::populate($config, new DotEnvLoader($projectRoot, 'custom.env'));` lets you point the loader to a specific directory or file without changing the default behaviour.
- **Helpful errors**
    When a non-nullable property cannot be populated, the library now throws `Env\Exception\MissingEnvironmentValue` with clear information about the missing variable.

Troubleshooting
---------------

[](#troubleshooting)

If you encounter any issues while using the library, please check the troubleshooting section of the documentation. If your issue is not covered in the troubleshooting section, please [create a new issue](https://github.com/didslm/php-env/issues/new) on the GitHub repository.

Contributing
------------

[](#contributing)

If you would like to contribute to the PHP Environment Variable Loader library, please read the [contributing guidelines](https://github.com/didslm/php-env/blob/main/CONTRIBUTING.md) before submitting a pull request.

Contact
-------

[](#contact)

If you have any questions or feedback on the library, please feel free to [contact me](https://linkedin.com/in/diarselimi).

License
-------

[](#license)

The PHP Environment Variable Loader library is open-source software licensed under the [MIT license](https://github.com/didslm/php-env/blob/main/LICENSE).

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance64

Regular maintenance activity

Popularity18

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~166 days

Recently: every ~250 days

Total

7

Last Release

199d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0a66a37c065b686ab7f65c7b1ec7038aa0a696272d7158d7245c25b11949986a?d=identicon)[didslm](/maintainers/didslm)

---

Top Contributors

[![didslm](https://avatars.githubusercontent.com/u/8136247?v=4)](https://github.com/didslm "didslm (11 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/didslm-env/health.svg)

```
[![Health](https://phpackages.com/badges/didslm-env/health.svg)](https://phpackages.com/packages/didslm-env)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
