PHP Datatype Validation
So, let’s say that you’re writing a function or some sort of script. and you need to sure you’re only getting a certain datatype as a value. Then you realize that PHP has no actual datatype enforcement like C or C++. You can set any variable to contain any kind of structure (strings, integers, arrays, etc) and you can later change it to any other structure later on the fly. This can be problematic depending on what you need to do.
With the function you’re supposedly writing, let’s say you wanted to make sure a argument you’re accepting via the PHP call is an integer. Well you could perform and is_int() function call by yourself, but thats not the point. The point of functions is to reduce tedious/repeated code. I made a class that has a function inside it that does all the testing for you depending on what you, as a coder, is expecting. So passing “hi” into the validator when you specify you’re looking for an integer wouldn’t pass. It would pass if you stated you were looking for a string, though.
What’s also nifty is that i wrote it to accommodate for the event of expecting multiple kinds of datatypes. Like let’s say you want either an integer or a double (a decimal), you can simple combine them when you call the validator. If the value you give the validator is an integer or a double, it’ll spit back the datatype.
Okay, how do I have it check for datatypes? Every datatype that I could look for is assigned a number in base 2 (1, 2, 4, 8, 16, 32, etc) . I assign them base 2 values in hex, simple because I felt like it.
define(“SCRAPBOOK_DATATYPE_STR”, 0×01); // STRING
define(“SCRAPBOOK_DATATYPE_ARR”, 0×02); // ARRAY
define(“SCRAPBOOK_DATATYPE_INT”, 0×04); // INTEGER
define(“SCRAPBOOK_DATATYPE_DOUBLE”, 0×08); // DOUBLE
SCRAPBOOK_DATATYPE_STR is set to 1, _ARR is 2, etc etc. You get the point. When I call the function, the first argument/parameter is the value I need to test. The second is the (possible) datatype(s) I’m expecting. What you may or may not know about bit addition is that its very easy to figure out what bits are included. For example, the value of 6 is ONLY made up of 2 and 4, which in this are the bits that are assigned to _ARR and _INT. Using this logic, we can pass multiple datatypes we might get (for example, we want a number which might be an integer or a decimal) and the function can check to see if the datatype of the value I’m checking for matches one of the datatypes I’m looking for.
Here is an example of looking for one datatype and then two datatypes:
//first instantiate the object
$validate = new Validator();//would return SCRAPBOOK_DATATYPE_ARR
print $validate->validateDatatype(array(), SCRAPBOOK_DATATYPE_ARR).”\n”;//would return SCRAPBOOK_DATATYPE_INT
print $validate->validateDatatype(5, SCRAPBOOK_DATATYPE_INT|SCRAPBOOK_DATATYPE_DOUBLE).”\n”;
//would return SCRAPBOOK_DATATYPE_DOUBLE
print $validate->validateDatatype(5.3, SCRAPBOOK_DATATYPE_INT|SCRAPBOOK_DATATYPE_DOUBLE).”\n”;
As you can see, the pipe (|) is used to add multiple of these bits together. The function would return the bit of the dataype found. this way if you’re expecting two datatypes, you can validate it and then see the exact type that was actually sent in.
Now how do i actually do the tests? well in the function I have something like this:
function validateDatatype($value = NULL, $expectedtypes = SCRAPBOOK_DATATYPE_STR)
{
//are we testing if this datatype exists? || now we test!
if ( ($expectedtypes & SCRAPBOOK_DATATYPE_STR) && (is_string($value)) )
return SCRAPBOOK_DATATYPE_STR;
if ( ($expectedtypes & SCRAPBOOK_DATATYPE_ARR) && (is_array($value)) )
return SCRAPBOOK_DATATYPE_ARR;
if ( ($expectedtypes & SCRAPBOOK_DATATYPE_INT) && (is_int($value)) )
return SCRAPBOOK_DATATYPE_INT;
if ( ($expectedtypes & SCRAPBOOK_DATATYPE_OBJECT) && (is_object($value)) )
return SCRAPBOOK_DATATYPE_OBJECT;
Essentially, i’m testing to see if we’re asking for each datatype, if we are asking for that datatype, i perform a function call to see if it actually is. If it’s not it keeps going until it returns something. if it gets to the end of the function, that means it didn’t validate, so false is returned. The (x & y) test basically checks to see if bit y exists within value x. So again, for example, lets say you have a number 13. The only way to get a number 13 through this method is with numbers 8, 4 and 1 (8+4+1 = 13). So if you did (13 & 4) it’d return true. So on and so forth.
Ok, now that I gave you little snippets, check out the entire script so you can see how it all blends together.