Programming in Hack
Alejandro Marcu
Dutch PHP Conference 2016
2
 Started programming Logo at
8 years old
 Then moved to Basic, Turbo
Pascal, C++, Java
 2001 – 2004 Various
programming jobs in
Argentina
 2004 – 2008: TopCoder
 2009 – 2015: Facebook
Alejandro Marcu
3
1. Introduction
2. Hack Types
3. Collections
4. Async
5. XHP
6. Hack Codegen
What You Will Learn Today
Introduction
5
HHVM history
 2007 - Hackathon project: convert PHP to c++
 2009 – HipHop (HPHPc) in prod in Facebook
 2010 – Open sourced HipHop for PHP
 2010 – Started the HHVM project
 2013 – HHVM in prod in Facebook
 2013 – HHVM open sourced
6
 In 2014 Wikipedia migrated to HHVM:
 CPU load went from 50% to 10%
 Mean page save time went 6s to 3s
 Average page load time went from 1.3s to 0.9s
 PHP 7 performance is close to HHVM
Example of migration to HHVM
7
 Started in 2012 as “strict-mode” project
 Grew to be much more than that
 Open sourced in 2014
 As of now, HHVM is the only execution engine that
runs Hack
 Totally interoperable with PHP, allowing for a gradual
migration
Hack
8
 Analyzes Hack programs statically
 Monitors files changes in the background
 Extremely fast, even in huge code bases
 Can even be used by IDEs to autocomplete code
Typechecker
Typechecker
hacklang.org/tutorial/
10
 Type annotations
 Collections
 Async
 XHP
 Attributes
 Constructor parameter
Promotion
 Enums
 Generics
Hack only features
 Operators
 Lambda
 Null safe
 XHP attribute access
 Pipe operator
 Shapes
 Trait and interfaces
requirements
 Type aliases
11
 References
 Global statements
 Top level code
 Variable variables
 Dynamic properties
 Pseudo-functions
 isset
 empty
 Unset
 goto
 Etc..
Unsupported features in Hack
12
<?hh //strict  Type annotations required
 Can’t call PHP
 No top level code
 No references
<?hh // partial
or
<?hh
 The following is allowed but not checked:
• Calls to PHP
• Top level code
• References
 Type annotations not required but checked
if present
<?hh // decl  Not checked at all
 Functions and classes are indexed
 Use it only as the first step to migrate to
Hack
Typechecker modes
Type annotations
14
Types
 Primitive types
 bool
 int
 float
 string
 array
 resource
 Combined primitive types
 num (int or float)
 arraykey (int or string)
 Object types
 Name of classes or interfaces
Return types
void can be used only as a return type
Parameters
Nullable parameters
Local variables
Class attributes
Class attributes
Class attributes
Constructor argument promotion
Enums
Enums
Enums
Generics
Arrays with generics
Collections
Vector
0-indexed list
Map
Keys: int or str.
Values: anything
Set
Unique elements
Collections overview
Vector
Map
Set
Immutability
Immutability
Immutability
Async
37
 Single thread blocking
 Single thread non-blocking
 Multithread
Threads
Single Thread blocking example
Single Thread blocking example
Single Thread blocking example
Async example
Async example
Async example
44
 Use the keyword async before function
 The return type must be Awaitable<…>
 Call async from async using await
 Call async from non-async using HHAsiojoin
 Make parallel calls using helpers in HHAsio
(such as v,vm,m,mf, etc.)
Async
XHP
46
What is XHP?
 XHP is used to generate HTML in PHP or Hack
 No templating
 No string concatenating
 Represent HTML as a tree of objects
 Familiar XML syntax
 Secure
 Strong validations
 Extensible
Hello World
composer.json
xhp.php
Hello World
Adding children
Custom class definition
Inheriting and transferring attributes
Async components
53
XHP parser transforms it into regular Hack code:
 XHP class names are replaced:
 First char colon is replaced with xhp_
 Other colons are replaced with two underscores
 Hyphens are replaced with an underscore
 E.g. :ui:nav:header is replaced with
xhp_ui__nav__header
XHP internals
54
 XHP tags are replaced with constructors. E.g.
will be replaced with:
XHP internals
Hack Codegen
56
Why code generation?
 Code generation: writing code that writes code
 Higher level of abstraction
 Generate boilerplate code
 Generate code from almost static data in database
 Replace usage of __call
57
 Easy to create files, functions, classes, methods,
arrays, collections, etc
 Support for partially generated files
 Files can be signed
 No string concatenation
 No need to indent or add spaces
 https://github.com/facebook/hack-codegen/
 Composer: require facebook/hack-codegen
Hack Codegen
Hack Codegen example
Hack Codegen example output
ORM code generation
ORM code generation
ORM code generation
ORM code generation
Learning Resources
65
Learning resources
 http://hacklang.org/ : installation, tutorial, docs
 “Hack & HHVM” book
Contact Information
amarcu@gmail.com
/alejandro.marcu
/alejandromarcu
@AlejandroMarcu
/in/alejandromarcu

Programming in hack

Editor's Notes

  • #17 class Demo { public function bar(array $a): int { return $a[0]; } }
  • #48 echo <div>Hello World!!</div>;
  • #49 function render(string $name): XHPRoot { return <div>Hello World, {$name}!!</div>; } echo render('PHP Conference');
  • #50 $select = <select />; $options = Vector {'yes', 'no'}; foreach ($options as $value) { $select->appendChild(<option value={$value}>{$value}</option>); }
  • #51 class :year-picker extends :x:element { attribute int from @required; attribute int to @required; public function render(): XHPRoot { $select = <select />; for ($i = $this->:from; $i <= $this->:to; $i++) { $select->appendChild( <option value={(string) $i}>{$i}</option> ); } return $select; } } echo <year-picker from={1960} to={1990} />;
  • #52 class :year-picker extends :x:element { attribute :select; use XHPHelpers; attribute int from @required; attribute int to @required; public function render(): XHPRoot { $select = <select />; for ($i = $this->:from; $i <= $this->:to; $i++) { $select->appendChild( <option value={(string) $i}>{$i}</option> ); } return $select; } } echo <year-picker from={1960} to={1990} name="selector" class="mystyle" />;
  • #53 class :user:link extends :x:element { attribute int userid @required; use XHPAsync; protected async function asyncRender(): Awaitable<XHPRoot> { $user = await get_user_data($this->:userid); return <a href={$user['link']} class="myclass"> {$user['name']} </a>; } } echo <user:link userid={26}/>;
  • #55 echo <div class="myclass">Hello World!!</div>; Will be replaced with echo new xhp_div( array('class' => 'myclass'), array('Hello World!!') );