Does PHP have structs, typedefs, and/or enums and if not what is the best implementation for them?

NoodleOfDeath picture NoodleOfDeath · Oct 1, 2010 · Viewed 35.2k times · Source

NOTICE!: This question is not a duplicate: I have provided below an alternative and highly useful enumeration method that accomplishes the desired effect - 11/13/2013

Is there a typedef keyword in PHP such that I can do something like:

typedef struct {

} aStructure;

or

typedef enum {
  aType1,
  aType2,
} aType;

EDIT

I eventually answered my own question below. I created a custom enum function that accomplishes exactly what I was asking for, but without type definition.

Answer

NoodleOfDeath picture NoodleOfDeath · Oct 3, 2010

I actually created my own kind of enum for PHP and it works just fine for what i need to do. no typedefs but its nice :D

Function enum($array, $asBitwise = false)



    /*
     * I formed another version that includes typedefs
     * but seeing how this hacky is not viewed as compliant
     * with programming standards I won't post it unless someone
     * wishes to request. I use this a lot to save me the hassle of manually
     * defining bitwise constants, but if you feel it is too pointless
     * for you I can understand. Not trying to reinvent the wheel here
     *
     */
    function enum(array $array, bool $asBitwise = false) {

        if(!is_array($array) || count($array) < 1)  return false;    // Error incorrect type

        $n = 0; // Counter variable
        foreach($array as $i) {
            if(!define($i, $n == 0 ? 0 : ($asBitwise ? 1 << ($n - 1) : $n))) return false;
            ++$n;
        } 
        return true; // Successfully defined all variables

    }



Usage (EXAMPLE):



    enum([
        'BrowserTypeUnknown',       // 0
        'BrowserTypeIE',            // 1
        'BrowserTypeNetscape',      // 2
        'BrowserTypeOpera',         // 3
        'BrowserTypeSafari',        // 4
        'BrowserTypeFirefox',       // 5
        'BrowserTypeChrome',        // 6
    ]); // BrowserType as an Increment

    $browser_type = BrowserTypeChrome;

    if($browser_type == BrowserTypeOpera) {
        // Make Opera Adjustments (will not execute)
    } else
    if($browser_type == BrowserTypeChrome) {
        // Make Chrome Adjustments (will execute)
    }

    enum([
        'SearchTypeUnknown',            // 0
        'SearchTypeMostRecent',         // 1 << 0
        'SearchTypePastWeek',           // 1 << 1
        'SearchTypePastMonth',          // 1 << 2
        'SearchTypeUnanswered',         // 1 << 3
        'SearchTypeMostViews',          // 1 << 4
        'SearchTypeMostActive',         // 1 << 5
    ], true); // SearchType as BitWise

    $search_type = SearchTypeMostRecent | SearchTypeMostActive;

    if($search_type & SearchTypeMostRecent) {
        // Search most recent files (will execute)
    }
    if($search_type & SearchTypePastWeek) {
        // Search files from the past will (will not execute)
    }

    if($search_type & SearchTypeMostActive) {
        // Search most active files AS WELL (will execute as well)
    }