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 权威指南
集合
101
常类似。这种情况下,如果
get_items()
的任何调用者试图修改它返回的
Vector
,一个
“非法操作异常(
InvalidOperationException
)”将会被抛出,非常清晰地向你展示什么
需要被修改。
5.3
使用集合
HHVM
,集合类甚至可以在常规的
PHP
文件中进行使用(换句话说,就是非
Hack
文件)。
你必须在这些类名前面使用
HH
的命名空间前缀(例如
HH/Vector
),然
Hack
文件中,
这些命名空间并非必需。
使用集合的代码看起来和使用数组的代码几乎一样。集合在语言和运行环境中都是内置
的,所以它们可以和许多已经在数组中使用的语言结构体无缝地对接工作。我们将在本
节中对它们一一学习。每个集合类都有一个功能齐全的面向对象的接口,我们在这里(在
5.4
节)将会看到其中最重要的部分。
5.3.1
字面量语法
Hack
对创建一个集合类实例添加了特殊的语法,我们称之为集合字面量语法。它包括类
名,紧随其后的是大括号括起来的项目列表。这些项目用逗号进行分隔。在
Map
字面量中,
每个项目都是一个键,紧跟着
=>
然后是值,这和
PHP
数组的字面量语法是一致的:
$vector = Vector {'one', 'two', 'three'};
$map = Map {'one' => 1, 'two' => 2, 'three' => 3};
$set = Set {'one', 'two', 'three'};
$pair = Pair {'one', 'two'};
集合字面量语法和常规 ...
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