Download the PHP package eloquent/constance without Composer
On this page you can find all versions of the php package eloquent/constance. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download eloquent/constance
More information about eloquent/constance
Files in eloquent/constance
Package constance
Short Description PHP constants as enumerations.
License MIT
Homepage https://github.com/eloquent/constance
Informations about the package constance
Constance
PHP constants as enumerations.
Installation and documentation
- Available as Composer package eloquent/constance.
- API documentation available.
What is Constance?
Constance is a library for representing PHP constants as an enumerated type. Constance supports both class-level and global constants, and is built upon a powerful enumeration library 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 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 directly.
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
This example demonstrates how to define an enumeration of PDO attributes:
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:
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:
Global constants
This example demonstrates how to define an enumeration of PHP error levels:
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 helper methods for dealing
with such sets of constants with bitwise values, which will be discussed in a
separate section.
Bitwise logic
Constance provides some methods for dealing with sets of constants that have bitwise values. The PHP error levels 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
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:
A bit-mask can also be generated for an arbitrary set of enumeration members, like so:
$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
will now contain a boolean value indicating whether E_ALL
includes strict standards warnings for the current run-time environment.