PHPackages                             samcrosoft/constantaccess - 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. samcrosoft/constantaccess

ActiveLibrary

samcrosoft/constantaccess
=========================

This library makes it possible to create and read (access) constants using an array-like object or a typical class object to make it possible to use constants in strings without the need of concatenation of strings

06PHP

Since Nov 28Pushed 10y ago1 watchersCompare

[ Source](https://github.com/samcrosoft/constantaccess)[ Packagist](https://packagist.org/packages/samcrosoft/constantaccess)[ RSS](/packages/samcrosoft-constantaccess/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Build Status](https://camo.githubusercontent.com/970bb8de75c2725e17482b074a7493f10be95ece7076f7c8f877df1e9e192cbb/68747470733a2f2f7472617669732d63692e6f72672f73616d63726f736f66742f636f6e7374616e746163636573732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/samcrosoft/constantaccess)

constantaccess
==============

[](#constantaccess)

This library makes it possible to create and read (access) constants using an array-like object or a typical class object to make it possible to use constants in strings without the need of concatenation of strings

Example
-------

[](#example)

Create an object of the constant access library and then start creating or reading constants

```
$oConst = new Samcrosoft\ConstantAccess\ConstantAccess();

```

Instantiating The Constant Class With An Array
----------------------------------------------

[](#instantiating-the-constant-class-with-an-array)

you can supply an array key=&gt;pair values for constants to create, **NOTE: this array should be a 1 dimensional array**

e.g

```
	$aData = array(
					'const1'  => 2,
					'const2'  => 22,
					'_const3' => 'another constant'
				   );

 // then create it now as

 $oConst = new Samcrosoft\ConstantAccess\ConstantAccess($aData);
```

### Creating Constants Using Object Method or Array Access

[](#creating-constants-using-object-method-or-array-access)

```
$oConst->CONST1 = 23;               This creates a constant called CONST1
$oConst['CONST2'] = "constant 2";   This creates another constant called CONST2
```

### Throwing Exceptions

[](#throwing-exceptions)

To create a valid php constant, the following conditions must be met

- A constant key can only be a valid php variable name
- A Constant value can only be a value of scalar type,

Before creating a constant, the above tests are conducted, hence there are possibilities of exceptions been thrown if any of the above listed conditions are not adhered to or met

The ConstantAccess **dies** silently i.e it does not throw any exception by default but for good programming practice, you should handle all possibilites of exceptions

### Enabling Exceptions

[](#enabling-exceptions)

You can enable the exceptions in two(2) ways,

- At the creation of the class object
- By using the **setThrowException** method

**e.g**

 ```
 1 - $oConst = new Samcrosoft\ConstantAccess\ConstantAccess(array(), true);
 2 - $oConst->setThrowException(false);

```

### Default Return Value

[](#default-return-value)

If a constant is not defined, you can configure a default return value that is returned in that case instead of breaking ur application

```
 e.g To set the default return value to null, you do the following

 $oConst->setValueReturnIfNotDefined(null);

 hence, $oConst->UNDEFINED; would return null
```

### Reading / Accessing Constants

[](#reading--accessing-constants)

```
print "this is a trial message with {$oConst->CONST1} or another {$oConst->CONST2}";
```

### Tests

[](#tests)

just run Phpunit in the folder where you have placed the library

\### Examples examples are located in the **/examples** folder

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/db25b0733689a0d6f2368e0c136d4f18cf001eed5ae253b900c73f478258248c?d=identicon)[samcrosoft](/maintainers/samcrosoft)

---

Top Contributors

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

### Embed Badge

![Health badge](/badges/samcrosoft-constantaccess/health.svg)

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

PHPackages © 2026

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