logo资料库

Hack.and.HHVM.Programming.Productivity.Without.Breaking.Things.pdf

第1页 / 共272页
第2页 / 共272页
第3页 / 共272页
第4页 / 共272页
第5页 / 共272页
第6页 / 共272页
第7页 / 共272页
第8页 / 共272页
资料共272页,剩余部分请下载后查看
Copyright
Table of Contents
Foreword
Preface
What Are Hack and HHVM?
Who This Book Is For
Philosophy
Program Types
Gradual Migration
How the Book Is Organized
Versions
Conventions Used in This Book
Safari® Books Online
How to Contact Us
Acknowledgments
Chapter 1. Typechecking
Why Use the Typechecker?
Setting Up the Typechecker
Autoload Everything
Reading Error Messages
Type Annotation Syntax
Function Return Types
Function Parameters
Properties
Hack’s Type System
Typechecker Modes
Code Without Annotations
Calling into PHP
Rules
Using Superglobals
Types of Overriding Methods
Property Initialization
Typed Variadic Arguments
Types for Generators
Fallthrough in switch Statements
Type Inference
Variables Don’t Have Types
Unresolved Types
Inference Is Function-Local
Refining Types
Refining Nullable Types to Non-Nullable
Refining Mixed Types to Primitives
Refining Object Types
Inference on Properties
Enforcement of Type Annotations at Runtime
Chapter 2. Generics
Introductory Example
Other Generic Entities
Functions and Methods
Traits and Interfaces
Type Aliases
Type Erasure
Constraints
Unresolved Types, Revisited
Generics and Subtypes
Arrays and Collections
Advanced: Covariance and Contravariance
Syntax
When to Use Them
Chapter 3. Other Features of Hack
Enums
Enum Functions
Type Aliases
Transparent Type Aliases
Opaque Type Aliases
Autoloading Type Aliases
Array Shapes
Lambda Expressions
Constructor Parameter Promotion
Attributes
Attribute Syntax
Special Attributes
Enhanced Autoloading
Integer Arithmetic Overflow
Nullsafe Method Call Operator
Trait and Interface Requirements
Silencing Typechecker Errors
Chapter 4. PHP Features Not Supported in Hack
References
The global Statement
Top-Level Code
Old-Style Constructors
Case-Insensitive Name Lookup
Variable Variables
Dynamic Properties
Mixing Method Call Syntax
isset, empty, and unset
Others
Chapter 5. Collections
Why Use Collections?
Collections Have Reference Semantics
Using Collections
Literal Syntax
Reading and Writing
Type Annotations for Collections
Core Interfaces
General Collection Interfaces
Specific Collection Interfaces
Concrete Collection Classes
Interoperating with Arrays
Conversion to Arrays
Use with Built-In and User Functions
Chapter 6. Async
Introductory Examples
Async in Detail
Wait Handles
Async and Callable Types
await Is Not an Expression
Async Generators
Exceptions in Async Functions
Mapping and Filtering Helpers
Structuring Async Code
Data Dependencies
Antipatterns
Other Types of Waiting
Sleeping
Rescheduling
Common Mistakes
Dropping Wait Handles
Memoizing Async Functions
Async Extensions
MySQL
MCRouter and memcached
cURL
Streams
Chapter 7. XHP
Why Use XHP?
Runtime Validation
Secure by Default
How to Use XHP
Basic Tag Usage
Attributes
Embedding Hack Code
Type Annotations for XHP
Object Interface
Validation
Creating Your Own XHP Classes
Attributes
children Declarations
Categories
Context
Async XHP
XHP Helpers
XHP Best Practices
No Additional Public API
Composition, Not Inheritance
Don’t Make Control Flow Tags
Distinguish Attributes from Children
Style Guide
Migrating to XHP
Converting Bottom-Up
Getting Around XHP’s Escaping
XHP Internals
The Parser Transformation
The Hack Library
Chapter 8. Configuring and Deploying HHVM
Specifying Configuration Options
Important Options
Server Mode
Warming Up the JIT
Repo-Authoritative Mode
Building the Repo
Deploying the Repo
The Admin Server
Chapter 9. hphpd: Interactive Debugging
Getting Started
Evaluating Code
The Execution Environment
Local Mode
Remote Mode
Using Breakpoints
Setting Breakpoints
Navigating the Call Stack
Navigating Code
Managing Breakpoints
Viewing Code and Documentation
Macros
Configuring hphpd
Chapter 10. Hack Tools
Inspecting the Codebase
Scripting Support
Migrating PHP to Hack
The Hackificator
Inferring and Adding Type Annotations
Transpiling Hack to PHP
Conversions
Unsupported Features
Index
About the Author
Hack & HHVM PROGRAMMING PRODUCTIVITY WITHOUT BREAKING THINGS Owen Yamauchi
Hack and HHVM How can you take advantage of the HipHop Virtual Machine (HHVM) and the Hack programming language, two new technologies that Facebook developed to run their web servers? With this practical guide, Owen Yamauchi—a member of Facebook’s core Hack and HHVM teams—shows you how to get started with these battle-tested open source tools. You’ll explore static typechecking and several other features that separate Hack from its PHP origins, and learn how to set up, configure, deploy, and monitor HHVM. Ideal for developers with basic PHP knowledge or experience with other languages, this book also demonstrates how these tools can be used with existing PHP codebases and new projects alike. ■ Learn how Hack provides static typechecking while retaining PHP’s flexible, rapid development capability ■ Write typesafe code with Hack’s generics feature ■ Explore HHVM, a just-in-time compilation runtime engine with full PHP compatibility ■ Dive into Hack collections, asynchronous functions, and the XHP extension for PHP ■ Understand Hack’s design rationale, including why it omits some PHP features ■ Use Hack for multitasking, and for generating HTML securely ■ Learn tools for working with Hack code, including PHP-to-Hack migration Owen Yamauchi is a software engineer at Facebook, where he works on the Hack and HHVM teams. Before joining the company in 2009, he worked as a software engineer at Apple and served as an intern at VMware. “ Hack is remarkable not only for the elegance and power of its type system and concurrency model, but because it provides existing PHP applications a thoughtful, iterative migration strategy that can be executed at scale. Yamauchi's survey of the language and its runtime is clear, expert, and essential. Highly recommended.” —Ori Livneh Principal Performance Engineer, Wikimedia Foundation PHP US $34.99 CAN $40.99 ISBN: 978-1-491-92087-9 Twitter: @oreillymedia facebook.com/oreilly
Hack and HHVM Programming Productivity Without Breaking Things Owen Yamauchi Boston
Hack and HHVM by Owen Yamauchi Copyright © 2015 Facebook, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://safaribooksonline.com). For more information, contact our corporate/ institutional sales department: 800-998-9938 or corporate@oreilly.com. Editor: Allyson MacDonald Production Editor: Melanie Yarbrough Copyeditor: Rachel Head Proofreader: Jasmine Kwityn September 2015: First Edition Revision History for the First Edition 2015-09-02: First Release Indexer: Ellen Troutman-Zaig Interior Designer: David Futato Cover Designer: Ellie Volkhausen Illustrator: Rebecca Demarest See http://oreilly.com/catalog/errata.csp?isbn=9781491920879 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Hack and HHVM, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-491-92087-9 [LSI]
Table of Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi 1. Typechecking. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Why Use the Typechecker? 1 Setting Up the Typechecker 2 Autoload Everything 3 Reading Error Messages 3 Type Annotation Syntax 4 Function Return Types 4 Function Parameters 5 Properties 6 Hack’s Type System 6 Typechecker Modes 14 Code Without Annotations 16 Calling into PHP 17 Rules 18 Using Superglobals 18 Types of Overriding Methods 19 Property Initialization 20 Typed Variadic Arguments 23 Types for Generators 24 Fallthrough in switch Statements 25 Type Inference 26 Variables Don’t Have Types 26 Unresolved Types 26 Inference Is Function-Local 28 iii
Refining Types 29 Refining Nullable Types to Non-Nullable 30 Refining Mixed Types to Primitives 32 Refining Object Types 32 Inference on Properties 35 Enforcement of Type Annotations at Runtime 36 2. Generics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Introductory Example 39 Other Generic Entities 41 Functions and Methods 41 Traits and Interfaces 42 Type Aliases 42 Type Erasure 43 Constraints 45 Unresolved Types, Revisited 47 Generics and Subtypes 49 Arrays and Collections 50 Advanced: Covariance and Contravariance 51 Syntax 51 When to Use Them 52 3. Other Features of Hack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Enums 57 Enum Functions 59 Type Aliases 60 Transparent Type Aliases 60 Opaque Type Aliases 61 Autoloading Type Aliases 64 Array Shapes 64 Lambda Expressions 66 Constructor Parameter Promotion 68 Attributes 69 Attribute Syntax 69 Special Attributes 71 Enhanced Autoloading 73 Integer Arithmetic Overflow 77 Nullsafe Method Call Operator 77 Trait and Interface Requirements 78 Silencing Typechecker Errors 80 iv | Table of Contents
4. PHP Features Not Supported in Hack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 References 83 The global Statement 84 Top-Level Code 84 Old-Style Constructors 85 Case-Insensitive Name Lookup 86 Variable Variables 86 Dynamic Properties 87 Mixing Method Call Syntax 88 isset, empty, and unset 88 Others 89 5. Collections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Why Use Collections? 93 Collections Have Reference Semantics 94 Using Collections 96 Literal Syntax 96 Reading and Writing 97 Type Annotations for Collections 102 Core Interfaces 102 General Collection Interfaces 106 Specific Collection Interfaces 107 Concrete Collection Classes 110 Interoperating with Arrays 112 Conversion to Arrays 112 Use with Built-In and User Functions 112 6. Async. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Introductory Examples 118 Async in Detail 121 Wait Handles 121 Async and Callable Types 123 await Is Not an Expression 124 Async Generators 125 Exceptions in Async Functions 127 Mapping and Filtering Helpers 129 Structuring Async Code 132 Data Dependencies 133 Antipatterns 135 Other Types of Waiting 140 Sleeping 140 Rescheduling 140 Table of Contents | v
Common Mistakes 143 Dropping Wait Handles 143 Memoizing Async Functions 145 Async Extensions 147 MySQL 147 MCRouter and memcached 151 cURL 153 Streams 154 7. XHP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Why Use XHP? 157 Runtime Validation 158 Secure by Default 159 How to Use XHP 161 Basic Tag Usage 161 Attributes 163 Embedding Hack Code 164 Type Annotations for XHP 164 Object Interface 165 Validation 167 Creating Your Own XHP Classes 168 Attributes 169 children Declarations 171 Categories 173 Context 174 Async XHP 175 XHP Helpers 176 XHP Best Practices 178 No Additional Public API 179 Composition, Not Inheritance 179 Don’t Make Control Flow Tags 180 Distinguish Attributes from Children 181 Style Guide 182 Migrating to XHP 182 Converting Bottom-Up 183 Getting Around XHP’s Escaping 184 XHP Internals 185 The Parser Transformation 185 The Hack Library 186 8. Configuring and Deploying HHVM. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Specifying Configuration Options 189 vi | Table of Contents
分享到:
收藏