PHPackages                             sebastiansulinski/dotenv - 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. sebastiansulinski/dotenv

AbandonedArchivedLibrary

sebastiansulinski/dotenv
========================

DotEnv package - work derived from vlucas/phpdotenv

v2.1.1(3mo ago)663.3k21MITPHPPHP ^7.1 || ^8.0

Since Aug 27Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/sebastiansulinski/dotenv)[ Packagist](https://packagist.org/packages/sebastiansulinski/dotenv)[ RSS](/packages/sebastiansulinski-dotenv/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (11)Used By (1)

DotEnv
======

[](#dotenv)

> **Warning**This package is deprecated and no longer maintained.

Package which enables to load environment variables from multiple .env files at multiple locations

This package is a work that derived from package published by [vlucas/phpdotenv](https://github.com/vlucas/phpdotenv) with some additional functionality such as handling multiple `.env` files and setting up variables using instance of the class.

[![Build Status](https://camo.githubusercontent.com/8f523b463b6b4b352dffd11db8757c07ada49b646f405fee82729b50db25eaac/68747470733a2f2f7472617669732d63692e6f72672f73656261737469616e73756c696e736b692f646f74656e762e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/sebastiansulinski/dotenv)

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

[](#installation)

Install package using composer

```
composer require sebastiansulinski/dotenv

```

Usage instructions
------------------

[](#usage-instructions)

To use the plugin you'll need to have at least one `.env` file i.e.

```
// .env

DB_HOST=localhost
DB_NAME=test
DB_USER=user
DB_PASS=password
```

You load all your `.env` files when instantiating the `SSD\DotEnv\DotEnv` object.

```
require "vendor/autoload.php";

use SSD\DotEnv\DotEnv;

$dotEnv = new DotEnv(__DIR__ . DIRECTORY_SEPARATOR . '.env');
```

You can pass a single `.env` file, path to a directory with `.env.*` files or multiple paths / directories

```
$dotEnv = new DotEnv(__DIR__ . DIRECTORY_SEPARATOR . '.env');
$dotEnv = new DotEnv(__DIR__);
$dotEnv = new DotEnv(
    __DIR__
    'another/path',
    'another/file/.env'
);
```

### Loading variables

[](#loading-variables)

To load process the variables there are two methods `load()` and `overload()`.

The `load()` method will only set the variables that do not already exist, while `overload()` will set them all - overwriting any existing ones.

```
$dotEnv = new DotEnv(__DIR__);

// will only set variables
// that are not already set
$dotEnv->load();
```

```
$dotEnv = new DotEnv(__DIR__);

// will set all variables from the files
// overwriting any duplicates
$dotEnv->overload();
```

### Required variables

[](#required-variables)

To ensure that your system has all necessary variables available you can use `required()` method, which takes either a single variable name or an array of required variables.

```
$dotEnv = new DotEnv(__DIR__);

// will only set variables
// that are not already set
$dotEnv->load();

// either a single variable
$dotEnv->required('DB_HOST');
```

```
$dotEnv = new DotEnv(__DIR__);

// will only set variables
// that are not already set
$dotEnv->load();

// or an array of variables
$dotEnv->required([
    'DB_HOST',
    'DB_NAME',
    'DB_USER',
    'DB_PASS'
]);
```

If any of the required variables does not exist in any of the `.env.*`files - system will throw a `RuntimeException`.

### Returning contents of `.env` file(s) as array

[](#returning-contents-of-env-files-as-array)

Use `toArray()` method to fetch the contents of the `.env` file(s), with or without setting up the environment variables.

```
$dotEnv = new DotEnv(__DIR__);

// will not set environment variables
$variables = $dotEnv->toArray();

var_dump($variables);

// ['DB_HOST' => '127.0.0.1', 'DB_NAME' => 'blog', ...]

// will set environment variables using load() method
$variables = $dotEnv->toArray(DotEnv::LOAD);

var_dump($variables);

// ['DB_HOST' => '127.0.0.1', 'DB_NAME' => 'blog', ...]

// will set environment variables using overload() method
$variables = $dotEnv->toArray(DotEnv::OVERLOAD);

var_dump($variables);

// ['DB_HOST' => '127.0.0.1', 'DB_NAME' => 'blog', ...]
```

### Obtaining value stored in the variable

[](#obtaining-value-stored-in-the-variable)

You can use a static `get()` method on the `DotEnv` object to retrieve the value stored in a given environment variable.

```
DotEnv::get('DB_HOST');
```

When you associate the string `true`, `false` with the variables within your `.env` file, they will automatically be converted to boolean `true` / `false` when using `DotEnv::get`. The same applies to the variable with `null` string, which will return `null` value.

If you specify a variable without any value associated (`MY_VARIABLE=`) - it will return an empty string `''`.

You can provide a second argument to the `get()` method, which will be returned if variable was not found. The default value can be of a `scalar` or a `Closure` type.

```
DotEnv::get('DB_HOST', 'localhost');

DotEnv::get('DB_HOST', function() {

    return DotEnv::get('ENVIRONMENT') == 'live' ? 'localhost' : 127.0.0.1;

});
```

### Checking if exists and equals

[](#checking-if-exists-and-equals)

You can check if variable exists by using `has()` and whether it stores a given value by using `is()` methods.

```
DotEnv::has('NON_EXISTENT_VARIABLE');
// false

DotEnv::is('ENVIRONMENT', 'live')
// true / false
```

### Setting variables

[](#setting-variables)

```
$dotEnv = new DotEnv(__DIR__);
$dotEnv->load();
$dotEnv->set('CUSTOM_VARIABLE', 123);
$dotEnv->required('CUSTOM_VARIABLE');
```

### Variable referencing

[](#variable-referencing)

If there is a variable that you'd like to inherit the value of you can use its name wrapped within the `${..}` i.e.

```
MAIL_SMTP=true
MAIL_USER=mail@mail.com
MAIL_PASS=password
MAIL_PORT=587

MAIL_API_KEY=${MAIL_PASS}
```

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance82

Actively maintained with recent releases

Popularity33

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity74

Established project with proven stability

 Bus Factor1

Top contributor holds 60% 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 ~425 days

Recently: every ~754 days

Total

10

Last Release

91d ago

Major Versions

0.0.1 → v1.0.02015-12-19

v1.2.0 → v2.0.02017-11-29

PHP version history (3 changes)0.0.1PHP &gt;=5.4.0

v1.2.0PHP ^7.1

v2.1.0PHP ^7.1 || ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2211203?v=4)[Sebastian Sulinski](/maintainers/sebastiansulinski)[@sebastiansulinski](https://github.com/sebastiansulinski)

---

Top Contributors

[![ssdtutorials](https://avatars.githubusercontent.com/u/18402239?v=4)](https://github.com/ssdtutorials "ssdtutorials (3 commits)")[![sebastiansulinski](https://avatars.githubusercontent.com/u/2211203?v=4)](https://github.com/sebastiansulinski "sebastiansulinski (2 commits)")

---

Tags

dotenvenvironmentenvironment-variablesenvironment-varsphpphp-varsphp71

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sebastiansulinski-dotenv/health.svg)

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

PHPackages © 2026

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