PHP是否具有结构或枚举,如果不是,它们的最佳实现是什么?

Noo*_*ath 25 php enums struct typedef

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

typedefPHP中是否有关键字,以便我可以执行以下操作:

typedef struct {

} aStructure;
Run Code Online (Sandbox Code Playgroud)

要么

typedef enum {
  aType1,
  aType2,
} aType;
Run Code Online (Sandbox Code Playgroud)

编辑

我最终在下面回答了我自己的问题(跳过第一个最近的回答).我创建了一个自定义enum函数,完全满足我的要求,但没有类型定义.

Noo*_*ath 21

我实际上为PHP创建了自己的枚举,它可以很好地满足我的需要.没有typedef但它很好:D

函数枚举($ 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

    }



用法(示例):



    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)
    }



  • 哦,小伙子,这很黑,虽然很聪明.我并没有真正看到这个好处.常量不会成功吗?你甚至可以用同样的方式声明它们. (16认同)
  • 我永远不会在生产代码中使用它. (6认同)
  • 没必要道歉.应用程序不是进行实验性枚举代码的好地方.最好只使用常量,这样其他必须在项目上工作的程序员才能理解它们是什么.此外,代码编辑软件将知道如何处理常量,但不会理解您的枚举.这就是为什么我永远不会想要这样的代码在生产中.想象一下,如果有什么东西坏了,不熟悉代码的技术人员必须弄清楚常量的拼写与其他地方的数组不匹配?PHP错误消息不会有帮助 (5认同)
  • 目前还没有人提到这一点,但这种方法的一个巨大的缺点是,它定义了全局常量并且默默地忽略了重复.因此,例如,如果文件定义按钮颜色(红绿蓝),则另一个文件定义背景颜色(蓝白色黑色),您将具有"蓝色"值,该值将为0或2,具体取决于加载枚举的时间,更不用说被随机名称污染的全局命名空间了.这个解决方案看起来很漂亮,但绝对不应该使用它,因为它将是一个维持的噩梦. (4认同)

dec*_*eze 17

不.

您必须使用数组,或者,如果您需要具有自定义类型的类,类和对象.