zinvapel / enumeration
枚举类
1.0.0
2020-12-16 19:27 UTC
Requires
- php: ^7.4 || ^8.0
This package is auto-updated.
Last update: 2024-09-17 03:32:33 UTC
README
使用示例
- 自己的枚举
<?php namespace App\Status; use Zinvapel\Enumeration\BaseEnumeration; class Status extends BaseEnumeration { public const ACTIVE = 'active'; public const INACTIVE = 'inactive'; protected $names = [ self::ACTIVE => 'Active', self::INACTIVE => 'Inactive', ]; /** * @return bool */ public function isActive(): bool { return $this->eq(self::ACTIVE); } }
- 在代码中
... $status = new Status(Status::INACTIVE); if ($status->neq(new Status(Status::ACTIVE))) { // do something with not active status } ...
- 使用静态构造函数
<?php namespace App\Status; use Zinvapel\Enumeration\BaseEnumeration; class Status extends BaseEnumeration { public const ACTIVE = 'active'; protected $names = [ self::ACTIVE => 'Active', ]; /** * @return Status */ public static function active(): Status { return new self(self::ACTIVE); } }