PHP是否具有结构,typedef和/或枚举,如果没有,则最佳的实现是什么? [英] Does PHP have structs, typedefs, and/or enums and if not what is the best implementation for them?
问题描述
注意!:这个问题不是重复的:我在下面提供了一种可替代的,非常有用的枚举方法,可以实现预期的效果-2013年11月13日
PHP中是否有 typedef
关键字,这样我可以执行以下操作:
typedef struct {
} aStructure;
或
typedef enum {
aType1,
aType2,
} aType;
编辑
<我终于在下面回答了我自己的问题。我创建了一个自定义的
枚举
函数,该函数可以完全实现我所要求的功能,但没有类型定义。我实际上为PHP创建了自己的一种枚举,它对我需要做的工作很好。没有typedef,但是它很不错:D
函数枚举($ array,$ asBitwise = false)
/ *
*我形成了另一个包含typedefs
*的版本,但是看到这种hacky并不被视为兼容的
*具有编程标准,除非有人
*希望提出要求,否则我不会将其发布。我经常使用它来省去手动定义位常数的麻烦,但是如果您觉得
*对您来说太没有意义了,我可以理解。
*
* /
函数枚举(array $ array,bool $ asBitwise = false){
if(!is_array($ array)|| count($ array)< 1)返回false; //错误类型错误
$ n = 0; //计数器变量
foreach($ array as $ i){
if(!define($ i,$ n == 0?0:($ asBitwise?1<<($ n- 1):$ n)))返回false;
++ $ n;
}
返回true; //成功定义了所有变量
}
用法(示例):
枚举([
'BrowserTypeUnknown',// 0
'BrowserTypeIE',// 1
'BrowserTypeNetscape',// 2
'BrowserTypeOpera',// 3
'BrowserTypeSafari ',// 4
'BrowserTypeFirefox',// 5
'BrowserTypeChrome',// 6
]); //浏览器类型作为增量
$ browser_type = BrowserTypeChrome;
if($ browser_type == BrowserTypeOpera){
//进行歌剧调整(不会执行)
}否则
if($ browser_type == BrowserTypeChrome){
//进行Chrome调整(将执行)
}
枚举([
'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){
//搜索最新文件(将执行)
}
if($ search_type&SearchTypePastWeek){
//搜索过去的文件(将不执行)
}
if($ search_type&SearchTypeMostActive){
//搜索最活跃的文件为WELL(将执行以及
}
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.
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)
}
这篇关于PHP是否具有结构,typedef和/或枚举,如果没有,则最佳的实现是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!