Skip to Main Content
Hack 与HHVM 权威指南
book

Hack 与HHVM 权威指南

by Owen Yamauchi
March 2017
Beginner content levelBeginner
247 pages
3h 55m
Chinese
China Machine Press
Content preview from Hack 与HHVM 权威指南
Hack
的其他特性
67
function
takes_card_suit(CardSuit $suit) {
// ...
}
function
legacy_function(int $suit) {
$enum_suit = CardSuit::coerce($suit);
if
($enum_suit !==
null
) {
takes_card_suit($enum_suit);
}
}
这里是相关的所有方法。我们假设返回的枚举类型的名字为
ExampleEnum
assert(mixed $value): ExampleEnum
,将会返回一个转化为枚举类型的
$value
前提是这个
$value
对应枚举类型的基础类型,并且它是这个对应枚举类型的成
员。如果不能满足这两个条件的话,它将会抛出一个“非期待值(
Unexpected
ValueException
)”的异常。
assertAll(Traversable<mixed> $value): Container<ExampleEnum>
该函数将会
对给定的可遍历的参数(见
5.4.1
节的内容)中的每个值调用函数
assert()
。然后
将返回一个包含所有正确类型值的容器
Container
。如果这些值中的任何一个不是
枚举成员,这里将会抛出一个“非期待值”异常。
coerce(mixed $value): ?ExampleEnum
,此方法和
assert()
非常相似,但是对于
$value
不是枚举成员变量的情况,将会返回 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Reinventing the Organization for GenAI and LLMs

Reinventing the Organization for GenAI and LLMs

Ethan Mollick

Publisher Resources

ISBN: 9787111554844