Talk given at PHP World 2015 about the Hack language released by Facebook. A short history and look at it's key features as well as how Hack and PHP are evolving together.
2. Hack the Future - Jason McCreary - php[world] 2015
Talk the Talk
1. What is Hack?
2. Key Features of Hack
3. Unsupported PHP features in Hack
4. How can I use Hack?
5. Why Hack?
6. The Future
2
3. Hack the Future - Jason McCreary - php[world] 2015
But first…
A quick disclaimer.
3
4. Hack the Future - Jason McCreary - php[world] 2015
"Some things are going to be said."
4
5. Hack the Future - Jason McCreary - php[world] 2015
"This is not a talk to push Hack."
5
6. Hack the Future - Jason McCreary - php[world] 2015
"This is not a talk to shit on PHP."
6
7. Hack the Future - Jason McCreary - php[world] 2015
"This is a talk about the evolution of a language."
7
8. Hack the Future - Jason McCreary - php[world] 2015
What is Hack?
A very brief history.
8
9. Hack the Future - Jason McCreary - php[world] 2015
Brief History
• In 2009, Facebook released HipHop - a PHP compiler
• In 2010, Facebook introduced new features into PHP
• In 2012, Facebook began working on Hack
• In 2013, Facebook released HHVM - a HipHopVM
• In 2014, Facebook released Hack
9
10. Hack the Future - Jason McCreary - php[world] 2015
So what is Hack?
"Hack is a programming language for HHVM that
interoperates seamlessly with PHP."
10
11. Hack the Future - Jason McCreary - php[world] 2015
So what is Hack, really?
"Hack is a superset of PHP that runs on HHVM which
provides type checking."
11
12. Hack the Future - Jason McCreary - php[world] 2015
WTF Facebook?
Just use PHP.
12
13. Hack the Future - Jason McCreary - php[world] 2015
"Well, they do."
13
14. Hack the Future - Jason McCreary - php[world] 2015
"Facebook has millions of lines of PHP."
14
15. Hack the Future - Jason McCreary - php[world] 2015
"Instead of a rewrite or waiting or influencing the PHP
community, they made Hack."
15
16. Hack the Future - Jason McCreary - php[world] 2015
"The goal being to write cleaner, safer, and
refactorable code."
16
17. Hack the Future - Jason McCreary - php[world] 2015
Key Features
A deeper look at some of the features of Hack.
17
18. Hack the Future - Jason McCreary - php[world] 2015
Key Features in Hack
• Type Annotations
• Additional types
• Generics and Nullable
• Asynchronous programming
• Syntactic sugar
18
19. Hack the Future - Jason McCreary - php[world] 2015
Type Annotations
<?hh
// parameter type annotations
function output(string $str) {
echo 'Hello, ' . $str;
}
// return type annotations
function get_output(string $str): string {
return 'Hello, ' . $str;
}
19
20. Hack the Future - Jason McCreary - php[world] 2015
"Hack is not a statically typed language."
20
21. Hack the Future - Jason McCreary - php[world] 2015
"Hack allows more type hinting than PHP."
21
22. Hack the Future - Jason McCreary - php[world] 2015
"HHVM performs type checking before runtime which
ensures your code is type safe."
22
26. Hack the Future - Jason McCreary - php[world] 2015
The problem with array
<?php
// indexed, starting at 0
$arr1 = array(1, 2, 3);
// indexed, starting at 1
$arr2 = array(1 => 1, 2, 3);
// keyed, mix of integer keys and string keys
$arr3 = array("foo" => 1, 73 => 2, "bar" => 3);
// all array values are the same
26
27. Hack the Future - Jason McCreary - php[world] 2015
The problem with array
"Primitive obsession is a code smell!"
27
28. Hack the Future - Jason McCreary - php[world] 2015
The problem with array
"In PHP, arrays are primitives (i.e. non-objects)."
28
29. Hack the Future - Jason McCreary - php[world] 2015
The problem with array
$ php -r 'var_dump(is_object(array()));'
bool(false)
29
30. Hack the Future - Jason McCreary - php[world] 2015
Collections
• Vector - an ordered, index collection
• Map - a key-based collection
• Set - an unordered, uniquely value collection
• Pair - an immutable, indexed, 2 element collection
• Both immutable and mutable versions
30
33. Hack the Future - Jason McCreary - php[world] 2015
Shapes
<?hh
// fixed size and type
// keyed collection of mixed values
$sh = shape('x' => 3, 'y' => 5);
echo $sh['x'] . ',' . $sh['y']; // outputs 3,5
33
34. Hack the Future - Jason McCreary - php[world] 2015
Tuples
<?hh
// fixed size and type, index array of mixed values
$tup = tuple('3', 2, 3, 4, 'hi');
echo $tup[1]; // outputs 2
unset($tup[0]); // error
$tup[5] = 'new value'; // error
$tup[4] = 'change value'; // ok, string to string
34
35. Hack the Future - Jason McCreary - php[world] 2015
Custom Types
<?hh
// define your own types
type MyInt = int;
type Point = (int, int);
35
36. Hack the Future - Jason McCreary - php[world] 2015
Generics and Nullable
• Generics - ability to parameterize the type of a class
or method and declare it upon instantiation
• Nullable - ability to declare a type allows null
36
37. Hack the Future - Jason McCreary - php[world] 2015
Generics
<?hh
class Box<T> {
protected T $data;
public function __construct(T $data) {
$this->data = $data;
}
public function getData(): T {
return $this->data;
}
}
37
38. Hack the Future - Jason McCreary - php[world] 2015
Nullable
<?hh
function check_not_null(?int $x): int {
if ($x === null) {
return -1;
} else {
return $x;
}
}
38
39. Hack the Future - Jason McCreary - php[world] 2015
Asynchronous Programming
<?hh
async function helloAfter($name, $timeout): Awaitable<string> {
await SleepWaitHandle::create($timeout * 1000000);
echo sprintf("hello %sn", $name);
}
async function run() {
$joe = helloAfter('Joe', 3);
$mike = helloAfter('Mike', 1);
await GenArrayWaitHandle::create(array($joe, $mike));
}
run()->join();
// prints:
// hello Mike
// hello Joe
39
40. Hack the Future - Jason McCreary - php[world] 2015
Syntactic Sugar
• Constructor Argument Promotion
• Lambdas
40
41. Hack the Future - Jason McCreary - php[world] 2015
Constructor Argument Promotion
<?php
class Person {
private string $name;
private int $age;
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
}
41
42. Hack the Future - Jason McCreary - php[world] 2015
Constructor Argument Promotion
<?hh
class Person {
public function __construct(public string $name,
protected int $age) {}
}
42
45. Hack the Future - Jason McCreary - php[world] 2015
Unsupported PHP Features
The things Hack no likey.
45
46. Hack the Future - Jason McCreary - php[world] 2015
Unsupported PHP Features
• Alternative syntaxes: AND, OR, XOR, endif, etc.
• Dynamic features: eval, continue n, break n,
$$var, $obj->{$var}
• Top Level Code
• References
• Globals
• Case insensitive function calls
• Explicitly named constructors
46
47. Hack the Future - Jason McCreary - php[world] 2015
How can I use Hack?
What you need to do to start Hack’in.
47
48. Hack the Future - Jason McCreary - php[world] 2015
"Change <?php to <?hh"
48
49. Hack the Future - Jason McCreary - php[world] 2015
"You’re right, first install HHVM."
49
50. Hack the Future - Jason McCreary - php[world] 2015
"Then change <?php to <?hh!"
50
51. Hack the Future - Jason McCreary - php[world] 2015
Hack Modes
• Hack has three modes: strict, partial, and
decl
• strict - type checker will catch every type error
• partial - type checker checks code that uses types
• decl - allows strict code to call separate, legacy
code
• The default is partial
51
52. Hack the Future - Jason McCreary - php[world] 2015
Hack Modes
<?hh // strict
function example(int $x) {
echo "I'm so strict!";
}
52
53. Hack the Future - Jason McCreary - php[world] 2015
Why Hack?
Some reasons for using Hack.
53
54. Hack the Future - Jason McCreary - php[world] 2015
Reasons to Hack
• Code clarity
• Performance
• Language features, like Asynchronous Programming
• Evolution
54
55. Hack the Future - Jason McCreary - php[world] 2015
The Future
So bright, I gotta wear shades.
55
56. Hack the Future - Jason McCreary - php[world] 2015
Some things in PHP
• Variadic functions (PHP 5.6)
• Scalar type declarations (PHP 7)
• Return type declarations (PHP 7)
• Null coalesce operator (PHP 7)
• Deprecations: alternative syntax and named
constructors (PHP 7)
56
57. Hack the Future - Jason McCreary - php[world] 2015
Look familiar?
<?php
function add(int $x, int $y): int {
return $x + $y;
}
$nil = null;
$foo = $nil ?? add(2, 3);
57
58. Hack the Future - Jason McCreary - php[world] 2015
"Yeah, Hack had it first."
58
59. Hack the Future - Jason McCreary - php[world] 2015
"Hack, coming soon in PHP 7+."
59
60. Hack the Future - Jason McCreary - php[world] 2015
"This is evolution."
60
61. Hack the Future - Jason McCreary - php[world] 2015
Questions…
Ask now or tweet me @gonedark.
61