此问题已经在这里有了答案:
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
PHP中是否有typedef
关键字,这样我可以执行以下操作:
typedef struct {
} aStructure;
或
typedef enum {
aType1,
aType2,
} aType;
编辑
我最终在下面回答了我自己的问题。我创建了一个自定义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
}
用法(示例):
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模板文件,该文件也是可配置的,因此您可以提供自己的模板。
这是用phpunit覆盖的完整测试。
php-enums on github (feel free to fork)
用法:(有关更多详细信息,请参见@ use.php,或进行单元测试)
<?php
//require the library
require_once __DIR__ . '/src/Enum.func.php';
//if you don't have a cache directory, create one
@mkdir(__DIR__ . '/cache');
EnumGenerator::setDefaultCachedClassesDir(__DIR__ . '/cache');
//Class definition is evaluated on the fly:
Enum('FruitsEnum', array('apple' , 'orange' , 'rasberry' , 'bannana'));
//Class definition is cached in the cache directory for later usage:
Enum('CachedFruitsEnum', array('apple' , 'orange' , 'rasberry' , 'bannana'), '\my\company\name\space', true);
echo 'FruitsEnum::APPLE() == FruitsEnum::APPLE(): ';
var_dump(FruitsEnum::APPLE() == FruitsEnum::APPLE()) . "\n";
echo 'FruitsEnum::APPLE() == FruitsEnum::ORANGE(): ';
var_dump(FruitsEnum::APPLE() == FruitsEnum::ORANGE()) . "\n";
echo 'FruitsEnum::APPLE() instanceof Enum: ';
var_dump(FruitsEnum::APPLE() instanceof Enum) . "\n";
echo 'FruitsEnum::APPLE() instanceof FruitsEnum: ';
var_dump(FruitsEnum::APPLE() instanceof FruitsEnum) . "\n";
echo "->getName()\n";
foreach (FruitsEnum::iterator() as $enum)
{
echo " " . $enum->getName() . "\n";
}
echo "->getValue()\n";
foreach (FruitsEnum::iterator() as $enum)
{
echo " " . $enum->getValue() . "\n";
}
echo "->getOrdinal()\n";
foreach (CachedFruitsEnum::iterator() as $enum)
{
echo " " . $enum->getOrdinal() . "\n";
}
echo "->getBinary()\n";
foreach (CachedFruitsEnum::iterator() as $enum)
{
echo " " . $enum->getBinary() . "\n";
}
输出:
FruitsEnum::APPLE() == FruitsEnum::APPLE(): bool(true)
FruitsEnum::APPLE() == FruitsEnum::ORANGE(): bool(false)
FruitsEnum::APPLE() instanceof Enum: bool(true)
FruitsEnum::APPLE() instanceof FruitsEnum: bool(true)
->getName()
APPLE
ORANGE
RASBERRY
BANNANA
->getValue()
apple
orange
rasberry
bannana
->getValue() when values have been specified
pig
dog
cat
bird
->getOrdinal()
1
2
3
4
->getBinary()
1
2
4
8
2)数据类型:
您可以使用反向的哈希/数组结构来“伪造”枚举:
$pretend_enum = array ( 'cent' => 1, 'nickel' => 2, 'dime' => 3 );
if ($pretend_enum[$value]) {
$encoded = $pretend_enum[$value];
} else {
echo "$value is not a valid coin";
}
“结构”通常是通过使用带有命名成员的哈希来伪造的:
$ceedee = array('title' => "Making Movies", 'artist' => "Dire Straights", 'tracks' => 12); echo "My favourite CD is " . $ceedee['title'];