Common integer representation over variable casting?

81 Views Asked by At

For a while i've been using: gettype(); to dynamically get the variable cast type, eg:

function Type ($Value = null){
 return gettype($Value);
}


echo Type(array()); // Echo array
echo Type (1); // Echo integer
echo Type(1.2); // echo doubble

Whereas, i'm looking for a method to have a common and known integer representation which is I believe is easier to validate over traditional strings.

Does anyone have a possible solution for this?

I have something like this:

function Type ($Value = null){
 $Type = gettype($Value);
 if ($Type === "array"){
   return 1;
 }elseif ($Type === "integer"){
   return 2;
 }
 // ...

}

This method gets the job done, but looks messy, and me being a kind of clean freak when it comes to my code... Is there something that looks more graceful?

3

There are 3 best solutions below

0
On

How about this one:

function typeAsNumber( $var = null ) {

   $validTypes = array( "boolean", 
                        "integer", 
                        "double", 
                        "string", 
                        "array",  
                        "object", 
                        "resource", 
                        "NULL", 
                        "unknown type" 
                      );

   return array_search( gettype( $var ), $validTypes);

}
1
On

I have no idea why you'd want to do this, as you've said.. You have been using this function, so you have obviously created code around the returned results.. So modifying such an important function can/might cause complications when rolling updates as this will include a mass code structure change, & a method to create a easy API, is to keep contiunity.. So if this code is available to the public, a drastic change will make it awkward for people (if using) to upgrade to another version.

If you have too.. Here is something I have just written up:

function Cast_Type ($Value = null){
    $Type = gettype($Value);
    switch ($Type){
        case "boolean":
            return 1;
            break;
        case "integer":
            return 2;
            break;
        case "double":
            return 3;
            break;
        case "string":
            return 4;
            break;
        case "array":
            return 5;
            break;
        case "object":
            return 6;
            break;
        case "resource":
            return 7;
            break;
        case "NULL":
            return 8;
            break;
        case "unknown type":
            return false;
            break;
    }
}

using the switch and case method makes it easy for users reading over your code & can validate the returns easier, as in.. A simple look will make it obvious that the resource type will return 7

0
On

When a function returns one of a set of enumerated values it is very common to define them as constants. This allows people to refer to them elsewhere in the code without knowing what the value is.

For example:

define('TYPE_BOOLEAN', 0);
define('TYPE_INTEGER', 1);
define('TYPE_FLOAT', 2);
define('TYPE_STRING', 3);
define('TYPE_ARRAY', 4);
define('TYPE_OBJECT', 5);
define('TYPE_RESOURCE', 6);
define('TYPE_NULL', 7);
define('TYPE_UNKNOWN', 8);

function type($var) {
    switch (gettype($var)) {
        case 'boolean':
            return TYPE_BOOLEAN;
        case 'integer':
            return TYPE_INTEGER;
        case 'double':
            return TYPE_FLOAT;
        case 'string':
            return TYPE_STRING;
        case 'array':
            return TYPE_ARRAY;
        case 'object':
            return TYPE_OBJECT;
        case 'resource':
            return TYPE_RESOURCE;
        case 'NULL':
            return TYPE_NULL;
    }
    return TYPE_UNKNOWN;
}

Now when you use the function you can write code that makes sense. Instead of type() just returning a nondescript integer it returns an integer that can be compared to a constant.

if (type($val) === TYPE_OBJECT) {
    $val = (array) $val;
}

All this said, however, it is a little unnecessary to do it like this. The gettype() function always returns the same value for the same type, so comparing the return value against something like 'object' does the job just as well.

if (gettype($val) === 'object') {
    $val = (array) $val;
}