2017 © Pedro Peláez
 

library 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

image

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

  • Saturday, November 28, 2015
  • by samcrosoft
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

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, (*2)

br/, (*3)

Example

Create an object of the constant access library and then start creating or reading constants, (*4)

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

br/, (*5)

Instantiating The Constant Class With An Array

you can supply an array key=>pair values for constants to create, NOTE: this array should be a 1 dimensional array, (*6)

e.g, (*7)

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

 // then create it now as

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

br/, (*8)

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

br/, (*9)

Throwing Exceptions

To create a valid php constant, the following conditions must be met, (*10)

  • 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, (*11)

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, (*12)

br/, (*13)

Enabling Exceptions

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

  • At the creation of the class object
  • By using the setThrowException method, (*15)

    e.g, (*16)

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

br/, (*17)

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, (*18)

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

 $oConst->setValueReturnIfNotDefined(null);

 hence, $oConst->UNDEFINED; would return null

br/, (*19)

Reading / Accessing Constants

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

br/, (*20)

Tests

just run Phpunit in the folder where you have placed the library, (*21)

br/, (*22)

Examples

examples are located in the /examples folder, (*23)

The Versions

28/11 2015

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar samcrosoft

arrayaccess constant constants constantaccess