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.
1 Answer 1
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!
-
\$\begingroup\$ Thank you for the answer. Both points taken, choosing function names and not iterating the constants array manually. I'm still learning PHP so I tend to overlook common functions. \$\endgroup\$Anonimista– Anonimista2012年07月04日 17:37:56 +00:00Commented Jul 4, 2012 at 17:37
mysql_field_type()? This seems like it accomplishes what you are trying to do. Which, if I understand you correctly, is simply to retrieve the data type from a MySQL result. If you are trying to limit these types to the list that you provided, I think the easiest way would be to check it while fetching it and throw an error or return FALSE if not of the right type, rather than try to filter it after already being set. \$\endgroup\$