HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Class to simulate enums in PHP

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
phpenumsclasssimulate

Problem

I have been reading this thread on Stack Overflow about simulating enums in PHP and it seems that the most common approach is to use class constants. My problem with that is I can't use it for type hints, so I have added a static function that checks if a value is defined as one of the constants.

class DataType
{
    const BIT = 'BIT';
    const TINYINT = 'TINYINT';
    const SMALLINT = 'SMALLINT';
    const MEDIUMINT = 'MEDIUMINT';
    const INT = 'INT';
    const INTEGER = 'INTEGER';
    const BIGINT = 'BIGINT';
    const FLOAT = 'FLOAT';
    const DECIMAL = 'DECIMAL';
    const NUMERIC = 'NUMERIC';
    const DATE = 'DATE';
    const TIME = 'TIME';
    const TIMESTAMP = 'TIMESTAMP';
    const DATETIME = 'DATETIME';
    const YEAR = 'YEAR';
    const CHAR = 'CHAR';
    const VARCHAR = 'VARCHAR';

    static public function Defines($const)
    {
        $cls = new ReflectionClass(__CLASS__);
        foreach($cls->getConstants() as $key=>$value)
        {
            if($value == $const)
            {
                return true;
            }
        }

        return false;
    }
}


Any thoughts on improving the code are welcome.

Solution

Why not just pass the data type you are looking for as a second argument to get_Type() then call the Defines() or similar method inside to verify it is the correct type? You can even provide a default data type should you use one more frequently.

function get_Type( $value, $DataType = VARCHAR ) {
    if( ! $this->Defines( $DataType ) ) { return FALSE; }

    //rest of method
}


Since you only want to test a given "DataType", I'd rewrite Defines() to use in_array() rather than loop through all the constants manually. Which BTW, you don't need to declare the $key=> bit in a foreach loop unless you are actually going to use the key.

function Defines( $const ) { return in_array( $const, $this->getConstants() ); }


I wrote this non-static, don't know if it needs to be static or not, but I tend to avoid static if at all possible.

I'd also like to point out that your method names conflict with PHP functions or aren't very descriptive. With the former you run the chance of your code conflicting with something else, or confusing people as to its purpose. With the latter you just confuse people. define() is a PHP function, so Defines() is close enough that it might cause issues or become confusing. get_Type() looks too much like you are fetching the data type rather than comparing it.

I hope this answer helps, if not then I'd add those last two comments to your actual question so more people might be able to help. That's what helped me to understand what you were trying to accomplish a lot better than the original explanation. If I actually understood it. Anyways, it seems much clearer now. Good Luck!

Code Snippets

function get_Type( $value, $DataType = VARCHAR ) {
    if( ! $this->Defines( $DataType ) ) { return FALSE; }

    //rest of method
}
function Defines( $const ) { return in_array( $const, $this->getConstants() ); }

Context

StackExchange Code Review Q#13093, answer score: 2

Revisions (0)

No revisions yet.