You are previewing Hack and HHVM.
O'Reilly logo
Hack and HHVM

Book Description

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.

Table of Contents

  1. Foreword
  2. Preface
    1. What Are Hack and HHVM?
    2. Who This Book Is For
    3. Philosophy
      1. Program Types
      2. Gradual Migration
    4. How the Book Is Organized
    5. Versions
    6. Conventions Used in This Book
    7. Safari® Books Online
    8. How to Contact Us
    9. Acknowledgments
  3. 1. Typechecking
    1. Why Use the Typechecker?
    2. Setting Up the Typechecker
      1. Autoload Everything
      2. Reading Error Messages
    3. Type Annotation Syntax
      1. Function Return Types
      2. Function Parameters
      3. Properties
    4. Hack’s Type System
      1. Typechecker Modes
      2. Code Without Annotations
      3. Calling into PHP
    5. Rules
      1. Using Superglobals
      2. Types of Overriding Methods
      3. Property Initialization
      4. Typed Variadic Arguments
      5. Types for Generators
      6. Fallthrough in switch Statements
    6. Type Inference
      1. Variables Don’t Have Types
      2. Unresolved Types
      3. Inference Is Function-Local
    7. Refining Types
      1. Refining Nullable Types to Non-Nullable
      2. Refining Mixed Types to Primitives
      3. Refining Object Types
      4. Inference on Properties
    8. Enforcement of Type Annotations at Runtime
  4. 2. Generics
    1. Introductory Example
    2. Other Generic Entities
      1. Functions and Methods
      2. Traits and Interfaces
      3. Type Aliases
    3. Type Erasure
    4. Constraints
    5. Unresolved Types, Revisited
    6. Generics and Subtypes
      1. Arrays and Collections
    7. Advanced: Covariance and Contravariance
      1. Syntax
      2. When to Use Them
  5. 3. Other Features of Hack
    1. Enums
      1. Enum Functions
    2. Type Aliases
      1. Transparent Type Aliases
      2. Opaque Type Aliases
      3. Autoloading Type Aliases
    3. Array Shapes
    4. Lambda Expressions
    5. Constructor Parameter Promotion
    6. Attributes
      1. Attribute Syntax
      2. Special Attributes
    7. Enhanced Autoloading
    8. Integer Arithmetic Overflow
    9. Nullsafe Method Call Operator
    10. Trait and Interface Requirements
    11. Silencing Typechecker Errors
  6. 4. PHP Features Not Supported in Hack
    1. References
      1. The global Statement
      2. Top-Level Code
    2. Old-Style Constructors
    3. Case-Insensitive Name Lookup
    4. Variable Variables
    5. Dynamic Properties
    6. Mixing Method Call Syntax
    7. isset, empty, and unset
    8. Others
  7. 5. Collections
    1. Why Use Collections?
    2. Collections Have Reference Semantics
    3. Using Collections
      1. Literal Syntax
      2. Reading and Writing
    4. Type Annotations for Collections
      1. Core Interfaces
      2. General Collection Interfaces
      3. Specific Collection Interfaces
      4. Concrete Collection Classes
    5. Interoperating with Arrays
      1. Conversion to Arrays
      2. Use with Built-In and User Functions
  8. 6. Async
    1. Introductory Examples
    2. Async in Detail
      1. Wait Handles
      2. Async and Callable Types
      3. await Is Not an Expression
      4. Async Generators
      5. Exceptions in Async Functions
      6. Mapping and Filtering Helpers
    3. Structuring Async Code
      1. Data Dependencies
      2. Antipatterns
    4. Other Types of Waiting
      1. Sleeping
      2. Rescheduling
    5. Common Mistakes
      1. Dropping Wait Handles
      2. Memoizing Async Functions
    6. Async Extensions
      1. MySQL
      2. MCRouter and memcached
      3. cURL
      4. Streams
  9. 7. XHP
    1. Why Use XHP?
      1. Runtime Validation
      2. Secure by Default
    2. How to Use XHP
      1. Basic Tag Usage
      2. Attributes
      3. Embedding Hack Code
      4. Type Annotations for XHP
      5. Object Interface
      6. Validation
    3. Creating Your Own XHP Classes
      1. Attributes
      2. children Declarations
      3. Categories
      4. Context
      5. Async XHP
      6. XHP Helpers
    4. XHP Best Practices
      1. No Additional Public API
      2. Composition, Not Inheritance
      3. Don’t Make Control Flow Tags
      4. Distinguish Attributes from Children
      5. Style Guide
    5. Migrating to XHP
      1. Converting Bottom-Up
      2. Getting Around XHP’s Escaping
    6. XHP Internals
      1. The Parser Transformation
      2. The Hack Library
  10. 8. Configuring and Deploying HHVM
    1. Specifying Configuration Options
      1. Important Options
    2. Server Mode
    3. Warming Up the JIT
    4. Repo-Authoritative Mode
      1. Building the Repo
      2. Deploying the Repo
    5. The Admin Server
  11. 9. hphpd: Interactive Debugging
    1. Getting Started
    2. Evaluating Code
    3. The Execution Environment
      1. Local Mode
      2. Remote Mode
    4. Using Breakpoints
      1. Setting Breakpoints
      2. Navigating the Call Stack
      3. Navigating Code
      4. Managing Breakpoints
    5. Viewing Code and Documentation
    6. Macros
    7. Configuring hphpd
  12. 10. Hack Tools
    1. Inspecting the Codebase
      1. Scripting Support
    2. Migrating PHP to Hack
      1. The Hackificator
      2. Inferring and Adding Type Annotations
    3. Transpiling Hack to PHP
      1. Conversions
      2. Unsupported Features
  13. Index