maisner/enum

Enum PHP 实现

v0.1.1 2018-11-15 13:24 UTC

This package is auto-updated.

Last update: 2024-09-29 05:21:24 UTC


README

Enum PHP 实现

用法

  • 实现 Enum 类
<?php declare(strict_types = 1);

use Maisner\Enum\AbstractEnum;

class TypeEnum extends AbstractEnum {

	public const TEMPERATURE = 'temperature';

	public const HUMIDITY = 'humidity';

	/**
	 * @return array|string[]
	 */
	protected static function allowedValues(): array {
		return [
			self::TEMPERATURE,
			self::HUMIDITY
		];
	}

	/**
	 * @return TypeEnum
	 */
	public static function TEMPERATURE(): self {
		return new self(self::TEMPERATURE);
	}

	/**
	 * @return TypeEnum
	 */
	public static function HUMIDITY(): self {
		return new self(self::HUMIDITY);
	}
}
  • 及其用法
$type = TypeEnum::TEMPERATURE();

$type->getValue();	//temperature
(string)$type;		//temperature