belca/support-constants
Composer 安装命令:
composer require belca/support-constants
包简介
Helper PHP classes for working with constants and enumerations.
README 文档
README
The documentation is actual for version 1.0.
Supporting classes for constants of PHP.
Named constants and enumerations are used for immutable values. The package classes contain functions for getting a list of constants and their values without thrown exceptions of PHP.
use Belca\Support\AbstractEnum; class MyConstants extends AbstractEnum { const DEFAULT = self::USER; const USER = 'user'; const ADMIN = 'administrator'; const CLIENT = 'client'; } // ... $constants = MyConstants::list(); // Output $constants: [ // 'USER' => 'user', // 'ADMIN' => 'superuser', // 'CLIENT' => 'client', // ] $default = MyConstants::getDefault(); // only using AbstractEnum // or $default = MyConstants::DEFAULT; // Output $default: 'user'
Introduction
Install the package using Composer or manually.
composer require belca/support-constants:1.*
Use one of the classes.
use Belca\Support\AbstractConstants; // or use Belca\Support\AbstractEnum;
Use their features!
AbstractConstants AbstractEnum
AbstractConstants
AbstractConstants is the abstract class for implementing a list of named constants and getting their values.
| Function | Description |
|---|---|
getConstants() |
Returns all constants of the class. |
list() |
The alias of the getConstants() function. |
getLastConstants() |
Returns an array of constants defined in the called class without constants of parrent classes. |
getParentConstants() |
Returns all constants of parent classes. |
getConst($name) |
$name - a name of constant. Returns a value of a given constant if it exists, else returns null. This is a safe method for calling constants, in otherwise when you calls undefined constants you will catch an error. |
isDefined($name) |
$name - a name of constant. Checks whether a given constant exists and is defined in the class. |
See the example below. Down there is implementing of the base (first) class of constants.
Example #1 Implementing the first (parent) class with constants
namespace AnyoneVendor\MyPackage\Enums; use Belca\Support\AbstractConstants; class Roles extends AbstractConstants { const USER = 'user'; const SUPERUSER = 'superuser'; const CLIENT = 'client'; const MODERATOR = 'moderator'; const SUPERMODERATOR = 'superuser'; }
After implementing the class you can get a list of constants from this class. Use list() or getConstants() to do this.
Example #2 Getting all constants from the parent class
$constants = Roles::list(); // or $constants = Roles::getConstants(); // Output $constants: [ // 'USER' => 'user', // 'SUPERUSER' => 'superuser', // 'CLIENT' => 'client', // 'MODERATOR' => 'moderator', // 'SUPERMODERATOR' => 'superuser', // ]
If you want to get one value then use the getConst() function. The function gets a case-sensitive name of constant. By convention, constant identifiers are always uppercase.
Example #3 Getting values of constants of the class
$user = Roles::getConst('USER'); // Output: 'user' $superuser = Roles::getConst('SUPERUSER'); // Output: 'superuser' $root = Roles::getConst('ROOT'); // Output: null, because it is not defined $user = Roles::getConst('user'); // Output: null, because the constant was defined in uppercase
The Roles class contains the constants for us.
You will probably want some other constants. Extend the class to solve the problem. This need exists when you uses third-party classes.
Example #4 Extending the parent class
namespace App\Enums; use AnyoneVendor\MyPackage\Enums\Roles as BaseRoles; class Roles extends BaseRoles { // Defines new constants const VIEWER = 'viewer'; const CHECKER = 'checker'; const TESTER = 'tester'; // Replaces old values of constants const SUPERUSER = 'root'; const SUPERMODERATOR = 'supermoderator'; }
See the examples of the new child class.
Example #5 Checking of existing constants
Roles::isDefined('SUPERUSER'); // true Roles::isDefined('ROOT'); // false
Example #6 Getting new constants
$constants = Roles::list(); // Output $constants: [ // 'USER' => 'user', // 'SUPERUSER' => 'root', // 'CLIENT' => 'client', // 'MODERATOR' => 'moderator', // 'SUPERMODERATOR' => 'superuser', // 'VIEWER' => 'viewer', // 'CHECKER' => 'checker', // ]
Example #6 Getting a value of the replaced constant
$superuser = Roles::getConst('SUPERUSER'); // 'root'
You can get some constant using the standard PHP syntax. If you try to take a undefined constant then you will catch an error.
If you are not sure about an existing constant then use the getConst() function and you will not catch an error.
Example 7 Getting values using the standard PHP function
$superuser = Roles::SUPERUSER; // 'root' $root = Roles::ROOT; // Error: Undefined class constant 'ROOT'
AbstractEnum
AbstractEnum is the abstract class for implementing a list of named constants and getting their values. It was extended from AbstractConstants.
In contract to the Belca\Support\AbstractConstants class, the Belca\Support\AbstractEnum uses a constant with default value.
| Function | Description |
|---|---|
getConstants() |
Returns all constants of the class. |
list() |
The alias of the getConstants() function. |
getLastConstants() |
Returns an array of constants defined in the called class without constants of parrent classes. |
getParentConstants() |
Returns all constants of parent classes. |
getConst($name) |
$name - a name of constant. Returns a value of a given constant if it exists, else returns null. This is a safe method for calling constants, in otherwise when you calls undefined constants you will catch an error. |
isDefined($name) |
$name - a name of constant. Checks whether a given constant exists and is defined in the class. |
getDefault() |
Returns the last defined default constant. |
The function that return a list of contains do not return the DEFAULT constant.
This class have a new function: the getDefault() function. The other functions are the same.
Example #8 Implementing the first (parent) class with a default value
namespace AnyoneVendor\MyPackage\Enums; use Belca\Support\AbstractEnum; class Roles extends AbstractEnum { const DEFAULT = self::USER; const USER = 'user'; const SUPERUSER = 'root'; const CLIENT = 'client'; }
Get the default value using the getDefault() function.
Example #9 Getting the default value
$default = Roles::getDefault(); // 'user'
Also you can get the default value using the PHP syntax.
Example #10 Getting the default value using the PHP syntax
$default = Roles::DEFAULT; // 'user'
You can redefine the default constant by means of extending class.
Example #11 Redefining the default constant
namespace App\Enums; use AnyoneVendor\MyPackage\Enums\Roles as BaseRoles; class Roles extends BaseRoles { const DEFAULT = self::UNREGISTRED; const UNREGISTERED = 'unregistered'; }
License
The package and other Belca components are open-sourced software licensed under the MIT license.
belca/support-constants 适用场景与选型建议
belca/support-constants 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「enum」 「enumeration」 「abstract」 「helper」 「support」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 belca/support-constants 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 belca/support-constants 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 belca/support-constants 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Enum type behavior for Yii2 based on class constants
A PHP Abstract Enum Class
PHP class to extend when building a WordPress plugin allowing you to follow smart plugin setup standards.
Compatibility layer for emulating enumerations in PHP < 8.1 and native enumerations in PHP >= 8.1
Enum type behavior and helper for Yii2, for PostgreSQL only
Enum libraries used by Zimbra Api
统计信息
- 总下载量: 11
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-01