PHPackages                             eloquent/constance - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. eloquent/constance

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

eloquent/constance
==================

PHP constants as enumerations.

0.1.1(12y ago)5294MITPHPPHP &gt;=5.3

Since Nov 11Pushed 12y ago1 watchersCompare

[ Source](https://github.com/eloquent/constance)[ Packagist](https://packagist.org/packages/eloquent/constance)[ Docs](https://github.com/eloquent/constance)[ RSS](/packages/eloquent-constance/feed)WikiDiscussions develop Synced 2mo ago

READMEChangelog (2)Dependencies (3)Versions (4)Used By (0)

Constance
=========

[](#constance)

*PHP constants as enumerations.*

[![The most recent stable version is 0.1.1](https://camo.githubusercontent.com/00ab3b01cd25bbb15440e909336e5c780774802ec3c04c3e29b3914e7ec8cb33/687474703a2f2f696d672e736869656c64732e696f2f3a73656d7665722d302e312e312d627269676874677265656e2e737667 "This project uses semantic versioning")](http://semver.org/)[![Current build status image](https://camo.githubusercontent.com/c508853162fc8e88d8e30826d8037102fffbd9a971fe3067ffa64099b974d1d3/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f656c6f7175656e742f636f6e7374616e63652f646576656c6f702e737667 "Current build status for the develop branch")](https://travis-ci.org/eloquent/constance)[![Current coverage status image](https://camo.githubusercontent.com/9c3cfefca5aa2f98813f5c5bad7b626943c6d1058f763518f1180203b3a2aa22/687474703a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f656c6f7175656e742f636f6e7374616e63652f646576656c6f702e737667 "Current test coverage for the develop branch")](https://coveralls.io/r/eloquent/constance)

Installation and documentation
------------------------------

[](#installation-and-documentation)

- Available as [Composer](http://getcomposer.org/) package [eloquent/constance](https://packagist.org/packages/eloquent/constance).
- [API documentation](http://lqnt.co/constance/artifacts/documentation/api/) available.

What is Constance?
------------------

[](#what-is-constance)

*Constance* is a library for representing PHP constants as an [enumerated type](http://en.wikipedia.org/wiki/Enumerated_type). *Constance* supports both class-level and global constants, and is built upon a powerful [enumeration library](https://github.com/eloquent/enumeration) to provide a rich feature set.

Amongst other uses, *Constance* allows a developer to:

- Type hint for a valid value as defined in a pre-existing set of constants. Examples include the PHP error levels, and PDO attributes.
- Retrieve information about a constant from its value, such as its name (useful for logging and debugging).
- Perform simple operations on sets of constants with [bitwise](http://en.wikipedia.org/wiki/Bitwise_operation) values, including retrieving members by bit-mask, or composing a bit-mask from an array of members.

*Constance* is most useful for dealing with pre-defined constants that the developer has no control over. If it is within the developer's control to implement a first-class enumeration, then the recommended approach is to use an [enumeration library](https://github.com/eloquent/enumeration) directly.

Implementing an enumeration of constants
----------------------------------------

[](#implementing-an-enumeration-of-constants)

*Constance* does not provide any concrete classes, but instead provides abstract base classes to make implementation of constant enumerations extremely simple. There are two abstract base classes - `AbstractClassConstant` for class-level constants, and `AbstractGlobalConstant` for global constants.

### Class-level constants

[](#class-level-constants)

This example demonstrates how to define an enumeration of [PDO attributes](http://www.php.net/manual/en/pdo.constants.php):

```
use Eloquent\Constance\AbstractClassConstant;

final class PdoAttribute extends AbstractClassConstant
{
    /**
     * The class to inspect for constants.
     */
    const CONSTANCE_CLASS = 'PDO';

    /**
     * The expression used to match constant names that should be included in
     * this enumeration.
     */
    const CONSTANCE_PATTERN = '{^ATTR_}';
}
```

As the example demonstrates, *Constance* enumerations are extremely simple to implement. The constant `CONSTANCE_CLASS` tells *Constance* which class to inspect for constants, and the optional constant `CONSTANCE_PATTERN` is a regular expression used to limit which of the constants defined in `CONSTANCE_CLASS` are defined as members of this enumeration. In this particular case, the expression limits the members to constants starting with `ATTR_`.

This enumeration can now be used to retrieve information about a PDO attribute constant when only its value is known at run-time:

```
$attribute = PdoAttribute::memberByValue(PDO::ATTR_ERRMODE);

echo $attribute->name();          // outputs 'ATTR_ERRMODE'
echo $attribute->qualifiedName(); // outputs 'PDO::ATTR_ERRMODE'
```

`PdoAttribute` can also be used as a type hint that will only accept valid PDO attributes. Note the special static call syntax for accessing members of an enumeration:

```
class MyConnection
{
    public function setAttribute(PdoAttribute $attribute, $value)
    {
        // ...
    }
}

$connection = new MyConnection;
$connection->setAttribute(PdoAttribute::ATTR_AUTOCOMMIT(), true);
$connection->setAttribute(PdoAttribute::ATTR_PERSISTENT(), false);
```

### Global constants

[](#global-constants)

This example demonstrates how to define an enumeration of [PHP error levels](http://www.php.net/manual/en/errorfunc.constants.php):

```
use Eloquent\Constance\AbstractGlobalConstant;

final class ErrorLevel extends AbstractGlobalConstant
{
    /**
     * The expression used to match constant names that should be included in
     * this enumeration.
     */
    const CONSTANCE_PATTERN = '{^E_}';
}
```

Global constants are even simpler to implement than class-level constants. The optional constant `CONSTANCE_PATTERN` is a regular expression used to limit which globally defined constants are defined as members of this enumeration. In this particular case, the expression limits the members to constants starting with `E_`.

The above enumeration will contain members for all the defined PHP error level constants, such as `E_NOTICE`, `E_WARNING`, `E_ERROR`, `E_ALL`, and `E_STRICT`. Note that *Constance* provides some limited [bitwise](http://en.wikipedia.org/wiki/Bitwise_operation) helper methods for dealing with such sets of constants with bitwise values, which will be discussed in a separate section.

Bitwise logic
-------------

[](#bitwise-logic)

*Constance* provides some methods for dealing with sets of constants that have [bitwise](http://en.wikipedia.org/wiki/Bitwise_operation) values. The [PHP error levels](http://www.php.net/manual/en/errorfunc.constants.php) enumeration above serves as a good example of such a set.

Assuming the `ErrorLevel` enumeration described above, the set of members included in `E_ALL` can be determined like so:

```
$members = ErrorLevel::membersByBitmask(E_ALL);
```

`$members` now contains an array of enumeration members representing all the error levels included in `E_ALL` for the current run-time environment. Conversely, the members that are *not* included can be determined just as easily:

```
$members = ErrorLevel::membersExcludedByBitmask(E_ALL);
```

A bit-mask can also be generated for an arbitrary set of enumeration members, like so:

```
$members = array(ErrorLevel::E_NOTICE(), ErrorLevel::E_DEPRECATED());
$bitmask = ErrorLevel::membersToBitmask($members);
```

`$bitmask` now contains a bit-mask that matches only `E_NOTICE` or `E_DEPRECATED`.

Lastly, checking whether a given enumeration member matches a bit-mask is also extremely simple:

```
$isStrictByDefault = ErrorLevel::E_STRICT()->valueMatchesBitmask(E_ALL);
```

`$isStrictByDefault` will now contain a boolean value indicating whether `E_ALL`includes strict standards warnings for the current run-time environment.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

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 ~79 days

Total

2

Last Release

4489d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/100152?v=4)[Erin](/maintainers/ezzatron)[@ezzatron](https://github.com/ezzatron)

---

Top Contributors

[![ezzatron](https://avatars.githubusercontent.com/u/100152?v=4)](https://github.com/ezzatron "ezzatron (12 commits)")

---

Tags

enumenumerationnamevalueconstant

### Embed Badge

![Health badge](/badges/eloquent-constance/health.svg)

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

###  Alternatives

[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49444.8M97](/packages/marc-mabe-php-enum)[cerbero/enum

Zero-dependencies package to supercharge enum functionalities.

359207.5k2](/packages/cerbero-enum)[cerbero/laravel-enum

Laravel package to supercharge enum functionalities.

18989.6k](/packages/cerbero-laravel-enum)[emreyarligan/enum-concern

A PHP package for effortless Enumeration handling with Laravel Collections 📦 ✨

21156.3k1](/packages/emreyarligan-enum-concern)[rexlabs/enum

Enumeration (enum) implementation for PHP

48482.2k2](/packages/rexlabs-enum)[thunderer/platenum

PHP enum library

36145.7k](/packages/thunderer-platenum)

PHPackages © 2026

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