Overview

Namespaces

  • GAubry
    • Enum

Classes

  • EnumAbstract
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo

Class EnumAbstract

A simple-to-use PHP class that provides the ability to emulate and create type-safe enumerations.

Definition:

class ColorEnum extends EnumAbstract {
    public static $RED;
    public static $GREEN;
    public static $BLUE;
}

Usage:

// One call per enumeration is necessary and sufficient,
// typically at the top of the program, during instantiation phase:
ColorEnum::buildInstances();

$color = ColorEnum::$RED;
var_dump((string)$color); // string(3) "RED"
var_dump(ColorEnum::$RED === ColorEnum::RED()); // true

function f (ColorEnum $color) {
    switch($color) {
        case ColorEnum::$RED:
        …
    }
    …
}
f(ColorEnum::$RED);

Copyright (c) 2013 Geoffroy Aubry <geoffroy.aubry@free.fr> Licensed under the GNU Lesser General Public License v3 (LGPL version 3).

Abstract
Namespace: GAubry\Enum
Copyright: 2013 Geoffroy Aubry <geoffroy.aubry@free.fr>
License: http://www.gnu.org/licenses/lgpl.html
Located at GAubry/Enum/EnumAbstract.php
Methods summary
protected
# __construct( string $sName )

Constructor.

Constructor.

Parameters

$sName
string
$sName Name of instance, used by __toString().
public static string
# buildInstances( )

Converts all static properties into EnumAbstract instances.

Converts all static properties into EnumAbstract instances.

Returns

string

Throws

BadMethodCallException
if called directly on EnumAbstract instead of derived class
public static array
# values( )

Returns an associative array containing all constants in the enum.

Returns an associative array containing all constants in the enum.

Returns

array
Associative array (name => instance, …)

Throws

BadMethodCallException
if called directly on EnumAbstract instead of derived class
public static array
# keys( )

Returns the names (or keys) of all of constants in the enum.

Returns the names (or keys) of all of constants in the enum.

Returns

array
Array of string

Throws

BadMethodCallException
if called directly on EnumAbstract instead of derived class
public static GAubry\Enum\EnumAbstract
# __callStatic( string $sName, array $aArgs )

Get enum instances with lazy instanciation. Triggered when invoking inaccessible methods in a static context.

Get enum instances with lazy instanciation. Triggered when invoking inaccessible methods in a static context.

Parameters

$sName
string
$sName name of the static property
$aArgs
array
$aArgs not used…

Returns

GAubry\Enum\EnumAbstract
enum instance with the specified name

Throws

DomainException
if not called on one of the static properties
BadMethodCallException
if called directly on EnumAbstract instead of derived class

SuppressWarnings(UnusedFormalParameter)

public string
# __toString( )

Returns the name of the instance.

Returns the name of the instance.

This magic method is used for setting a string value for the object. It will be used if the object is used as a string.

Returns

string
representing the object
final public
# __clone( )

Prevent cloning.

Prevent cloning.

Throws

RuntimeException
Cloning of this object isn't authorized!
Properties summary
private static integer $iCounter 0
#

Global static counter to ensure that each EnumAbstract instance is unique.

Global static counter to ensure that each EnumAbstract instance is unique.

private static array $aCache array()
#

A cache of all enum values to increase performance. Structure:

array(
    'class' => array(
        'key' => EnumAbstract instance,
        …
    ),
    …
)

A cache of all enum values to increase performance. Structure:

array(
    'class' => array(
        'key' => EnumAbstract instance,
        …
    ),
    …
)
private integer $iValue
#

A unique value to ensure that each EnumAbstract instance is unique.

A unique value to ensure that each EnumAbstract instance is unique.

See

GAubry\Enum\EnumAbstract::$iCounter
private string $sName
#

Name of enum instance, used by __toString().

Name of enum instance, used by __toString().

See

GAubry\Enum\EnumAbstract::__toString()
Type-safe PHP Enum API documentation generated by ApiGen 2.8.0