petrknap/enum
最新稳定版本:v3.0.1
Composer 安装命令:
composer require petrknap/enum
包简介
Enumerated type for PHP
README 文档
README
What is Enum?
In computer programming, an enumerated type (also called enumeration or enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called elements, members, enumeral, or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. In other words, an enumerated type has values that are different from each other, and that can be compared and assigned, but which are not specified by the programmer as having any particular concrete representation in the computer's memory; compilers and interpreters can represent them arbitrarily. -- Enumerated type - Wikipedia, The Free Encyclopedia
Why use Enums instead of Constants?
Because it is safer and less scary than using constants. Don't trust me? Let see at this code:
use PetrKnap\Enum\Readme\MyBoolean; $isTrue = function (int $myBoolean) { switch($myBoolean) { case MyBoolean::MY_TRUE: return true; case MyBoolean::MY_FALSE: return false; } }; var_dump($isTrue(MyBoolean::MY_TRUE)); // true - correct var_dump($isTrue(MyBoolean::MY_FALSE)); // false - correct var_dump($isTrue(0)); // none var_dump($isTrue(1)); // true - expected var_dump($isTrue(2)); // false var_dump($isTrue((int) true)); // true - expected var_dump($isTrue((int) false)); // none
And now the same code with enum instead of constants:
use PetrKnap\Enum\Readme\MyBoolean; $isTrue = function (MyBoolean $myBoolean): bool { switch($myBoolean) { case MyBoolean::MyTrue: return true; case MyBoolean::MyFalse: return false; } }; var_dump($isTrue(MyBoolean::MyTrue)); // true - correct var_dump($isTrue(MyBoolean::MyFalse)); // false - correct
Run composer require petrknap/enum to install it.
You can support this project via donation.
The project is licensed under the terms of the LGPL-3.0-or-later.
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0-or-later
- 更新时间: 2023-10-17