ngmy / enum
PHP 的枚举类型
0.7.0
2021-04-03 12:51 UTC
Requires
- php: ^7.3|^8.0
- ngmy/typed-array: ^0.11.0
This package is auto-updated.
Last update: 2024-09-11 20:04:58 UTC
README
PHP Enum 是 PHP 的枚举类型。
- 类似于 Java 的枚举类型接口
- 也提供类似于 Java 的枚举映射和集合
- 支持 PHPStan 和 Psalm 等静态分析。请参阅 示例
/** * @method static self FOO() * @method static self BAR() * @method static self BAZ() */ class Enum1 extends Ngmy\Enum\Enum { /** @enum */ private static $FOO; /** @enum */ private static $BAR; /** @enum */ private static $BAZ; } // Returns the enum constant of the specified name $foo = Enum1::valueOf('FOO'); $bar = Enum1::valueOf('BAR'); $baz = Enum1::valueOf('BAZ'); // You can also use magic factory methods $foo = Enum1::FOO(); $bar = Enum1::BAR(); $baz = Enum1::BAZ(); // Returns the name of this enum constant, exactly as declared in its enum declaration echo $foo->name() . PHP_EOL; // FOO echo $bar->name() . PHP_EOL; // BAR echo $baz->name() . PHP_EOL; // BAZ // Returns the name of this enum constant, as contained in the declaration echo $foo . PHP_EOL; // FOO echo $bar . PHP_EOL; // BAR echo $baz . PHP_EOL; // BAZ // Returns the ordinal of this enum constant echo $foo->ordinal() . PHP_EOL; // 0 echo $bar->ordinal() . PHP_EOL; // 1 echo $baz->ordinal() . PHP_EOL; // 2 // Returns true if the specified object is equal to this enum constant echo var_export($foo->equals($foo), true) . PHP_EOL; // true echo var_export($foo->equals(Enum1::valueOf('FOO')), true) . PHP_EOL; // true echo var_export($foo->equals($bar), true) . PHP_EOL; // false // You can also have the enum constant with a value /** * @method static self FOO() * @method static self BAR() * @method static self BAZ() */ class Enum2 extends Ngmy\Enum\Enum { /** @enum */ private static $FOO = 1; /** @enum */ private static $BAR = 2; /** @enum */ private static $BAZ = 3; public function getValue(): int { return self::${$this->name()}; } } echo Enum2::valueOf('FOO')->getValue() . PHP_EOL; // 1 echo Enum2::valueOf('BAR')->getValue() . PHP_EOL; // 2 echo Enum2::valueOf('BAZ')->getValue() . PHP_EOL; // 3
要求
PHP Enum 有以下要求
- PHP >= 7.3
安装
执行 Composer 的 require
命令
composer require ngmy/enum
文档
请参阅 API 文档。
许可证
PHP Enum 是开源软件,受 MIT 许可证 许可。