PHP是否具有结构,typedef和/或枚举,如果没有,则最佳的实现是什么? [重复]

问题描述 投票:25回答:6

此问题已经在这里有了答案:

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函数,该函数可以完全实现我所要求的功能,但没有类型定义。

php struct enums typedef
6个回答
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) }


17
投票
Nope.

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


1
投票
您可以对常量执行类似的操作,但是它与专用枚举不同。

1
投票
它们是一个名为SPL_Types的扩展名,但是此扩展名几乎没有可用的虚拟主机及其not maintained anymore。因此,最好的方法是将类用于结构。和枚举常量。也许借助普通的SPL扩展名的帮助,几乎在每个可用的php 5.X安装中,您都可以构建一些“邪恶的枚举黑客”]

0
投票
这里是一个用于处理php中类型安全枚举的github库:

[此库处理类的生成,类缓存,并实现了类型安全枚举设计模式,并提供了几种用于处理枚举的辅助方法,例如检索用于枚举排序的序数,或检索枚举组合的二进制值。

生成的代码使用一个普通的旧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


-3
投票
PHP有两种(计数它们–

2)数据类型:

  1. 作为文本存储的单个标量值,在可能的算术或逻辑上下文中将其转换为数字或布尔值。
  2. 第二个结构是由标量文本作为键的哈希(不是数组!)。如果键是数字值,则哈希的行为非常类似于数组,但如果是文本值,则其行为更类似于经典的perl哈希。

您可以使用反向的哈希/数组结构来“伪造”枚举:

$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'];

© www.soinside.com 2019 - 2024. All rights reserved.