PHP枚举常量

2024-01-03 81

在 PHP 中,虽然枚举类型(enum)可以包括 public、private、protected 的常量,但是由于它不支持继承的功能,因此在实践中 private 和 protected 的效果是相同的。由于用 enum 自身的常量表示条目,它们可当作静态值,用于绝大多数常量表达式。同时枚举也能使用 trait,行为和 class 一样。

一、枚举常量示例

枚举常量可以引用枚举条目:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?<a href="https://www.zzbaike.com/tag/php" title="查看所有文章关于 php" target="_blank">php</a>
enum Size
{
case Small;
case Medium;
case Large;
public const Huge = self::Large;
}
?>
<?<a href="https://www.zzbaike.com/tag/php" title="查看所有文章关于 php" target="_blank">php</a> enum Size { case Small; case Medium; case Large; public const Huge = self::Large; } ?>
<?php
enum Size
{
case Small;
case Medium;
case Large;
public const Huge = self::Large;
}
?>

二、Trait

枚举也能使用 trait,行为和 class 一样。 留意在枚举中 use trait 不允许包含属性。 只能包含方法、静态方法。 包含属性的 trait 会导致 fatal 错误。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
interface Colorful
{
public function color(): string;
}
trait Rectangle
{
public function shape(): string {
return "Rectangle";
}
}
enum Suit implements Colorful
{
use Rectangle;
case Hearts;
case Diamonds;
case Clubs;
case Spades;
public function color(): string
{
return match($this) {
Suit::Hearts, Suit::Diamonds => 'Red',
Suit::Clubs, Suit::Spades => 'Black',
};
}
}
?>
<?php interface Colorful { public function color(): string; } trait Rectangle { public function shape(): string { return "Rectangle"; } } enum Suit implements Colorful { use Rectangle; case Hearts; case Diamonds; case Clubs; case Spades; public function color(): string { return match($this) { Suit::Hearts, Suit::Diamonds => 'Red', Suit::Clubs, Suit::Spades => 'Black', }; } } ?>
<?php
interface Colorful
{
public function color(): string;
}
trait Rectangle
{
public function shape(): string {
return "Rectangle";
}
}
enum Suit implements Colorful
{
use Rectangle;
case Hearts;
case Diamonds;
case Clubs;
case Spades;
public function color(): string
{
return match($this) {
Suit::Hearts, Suit::Diamonds => 'Red',
Suit::Clubs, Suit::Spades => 'Black',
};
}
}
?>

三、常量表达式的枚举值

由于用 enum 自身的常量表示条目,它们可当作静态值,用于绝大多数常量表达式: 属性默认值、变量默认值、参数默认值、全局和类常量。 他们不能用于其他 enum 枚举值,但通常的常量可以引用枚举条目。

然而,因为不能保证结果值绝对不变,也不能避免调用方法时带来副作用, 所以枚举里类似 ArrayAccess 这样的隐式魔术方法调用无法用于静态定义和常量定义。 常量表达式还是不能使用函数调用、方法调用、属性访问。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// 这是完全合法的 Enum 定义
enum Direction implements ArrayAccess
{
case Up;
case Down;
public function offsetGet($val) { ... }
public function offsetExists($val) { ... }
public function offsetSet($val) { throw new Exception(); }
public function offsetUnset($val) { throw new Exception(); }
}
class Foo
{
// 可以这样写。
const Bar = Direction::Down;
// 由于它是不确定的,所以不能这么写。
const Bar = Direction::Up['short'];
// Fatal error: Cannot use [] on enums in constant expression
}
// 由于它不是一个常量表达式,所以是完全合法的
$x = Direction::Up['short'];
?>
<?php // 这是完全合法的 Enum 定义 enum Direction implements ArrayAccess { case Up; case Down; public function offsetGet($val) { ... } public function offsetExists($val) { ... } public function offsetSet($val) { throw new Exception(); } public function offsetUnset($val) { throw new Exception(); } } class Foo { // 可以这样写。 const Bar = Direction::Down; // 由于它是不确定的,所以不能这么写。 const Bar = Direction::Up['short']; // Fatal error: Cannot use [] on enums in constant expression } // 由于它不是一个常量表达式,所以是完全合法的 $x = Direction::Up['short']; ?>
<?php
// 这是完全合法的 Enum 定义
enum Direction implements ArrayAccess
{
case Up;
case Down;
public function offsetGet($val) { ... }
public function offsetExists($val) { ... }
public function offsetSet($val) { throw new Exception(); }
public function offsetUnset($val) { throw new Exception(); }
}
class Foo
{
// 可以这样写。
const Bar = Direction::Down;
// 由于它是不确定的,所以不能这么写。
const Bar = Direction::Up['short'];
// Fatal error: Cannot use [] on enums in constant expression
}
// 由于它不是一个常量表达式,所以是完全合法的
$x = Direction::Up['short'];
?>
  • 广告合作

  • QQ群号:4114653

温馨提示:
1、本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。邮箱:2942802716#qq.com(#改为@)。 2、本站原创内容未经允许不得转裁,转载请注明出处“站长百科”和原文地址。
PHP枚举常量
上一篇: PHP枚举方法