2. Agenda
What is the Hack Language ?
Hack Modes
Type Annotations
Nullable
Generics
Collections
Type Aliasing
Constructor Argument Promotion
PHP to Hack Conversion DEMO
3. Not on Agenda, but interesting
Async - asynchronous programming support via
async/await
XHP – an extension of the PHP and Hack syntax
where XML elements/blocks become valid
expressions
Lamba expressions – a more advanced version of PHP
closures
4. What is the Hack Language ?
Language for HHVM that interoperates with PHP
Developed and used by Facebook
Open-Sourced in March 2014
Basically, PHP with a ton of extra features
Hack = Fast development cycle of PHP + discipline of
strong typing
Combines strong typing with weak typing => gradual
typing (you can strong type only the parts you want)
Write cleaner/safer code while maintaining good
compatibility with existing PHP codebases
5. What is the Hack Language ? (II)
Hack code uses “<?hh” instead of “<?php”. Not
mandatory but, if you do it, you won’t be able to jump
between Hack and HTML anymore
Hack and PHP can coexist in the same project. Good if
you want an incremental switch to Hack.
PHP code can call Hack code and vice-versa
Closing tags (“?>”) are not supported
Naming your files “*.hh” can be a good practice
6. Hack Modes (I)
Intended for maximum flexibility converting PHP to
Hack
Helps when running “hackificator”
Each file can have only one Hack mode
Default is “partial”
Declared at the top: “<?hh // strict”
7. Hack Modes (II)
Strict:
<?hh // strict
Type checker will catch every error
All code in this mode must be correctly annotated
Code in strict mode cannot call non-Hack code.
9. Hack Modes (IV)
Decl:
<?hh // decl
used when annotating old APIs
Allows Hack code written in strict mode to call into legacy
code. Just like in partial mode, but without having to fix
reported problems
10. Type Annotations (I)
Readability by helping other developers understand the
purpose and intention of the code. Many used
comments to do this, but Hack formalizes it instead
Correctness by forbidding unsafe coding practices
Make use of automatic and solid refactoring tools of a
strong-typed language.
11. Type Annotations (II)
class MyExampleClass {
public int $number;
public CustomClass $myObject;
private string $theName;
protected array $mystuff;
function doStuff(string $name, bool $withHate): string {
if ($withHate && $name === 'Satan') {
return '666 HAHA';
}
return '';
}
}
13. Nullable
Safer way to deal with nulls
In type annotation, add “?” to the type to make it
nullable. e.g. ?string
Normal PHP code does allow some annotation:
public function doStuff(MyClass $obj)
But passing null to this results in a fatal error
So in Hack you can do this:
public function doStuff(?MyClass $obj)
It’s best not to abuse this feature, but to use it when you
really need it
14. Generics (I)
Allows classes and methods to be parameterized
Generics code can be statically checked for correctness
Offers a great alternative against using a top-level object + a
bunch of instanceof calls + type casts
In most languages, classes must be used as types. But Hack
allows primitives too
They are immutable: once a type has been associated, it can
not be changed
In Hack, objects can be used as types too
Nullable types are also supported
Interfaces and Traits support Generics too
See docs for more details
15. Generics (II)
class MyClass<K> {
private K $param;
public function __construct(K $param) {
$this->param = $param;
}
public function getParameter(): K {
return $this->param;
}
}
16. Collections (I)
Classes specialized for data storage and retrieval
PHP arrays are too general, they offer a “one-size-fits-all”
approach
In both Hack and PHP, arrays are not objects. But
Collections are.
Seeing the keyword "array" in a piece of code doesn't
make it clear how that array will be used
But each Collection class is best suited for specific
situations.
So code is clearer and, if used correctly, can be a
performance improvement
17. Collections (II)
They are traversable with “foreach” loops
Support bracket notations: $mycollection[$index]
Since they are objects, they are not copied when passed
around as parameters
They integrate and work very well with Generics
Immutable variants of the Collection classes also exist.
This is to ensure that they are not changed when passed
around
Classes are: Vector, Map, Set, Pair
Immutable collections: ImmVector, ImmMap, ImmSet
20. Type Aliasing
Just like PHP has aliasing support in namespaces, so
does Hack provide the same functionality for any type
Two modes of declaration:
type MyType = string;
newtype MyType = string;
The 2nd is called “opaque aliasing” and is only visible in
the same file
Composite types can also be declared:
newtype Coordinate = (float, float);
These are implemented as tuples, so that is how they must
be created in order to be used
21. Constructor Argument Promotion (I)
Before:
class User {
private string $name;
private string $email;
private int $age;
public function __construct(string $name, string $email, int $age)
{
$this->name = $name;
$this->email = $email;
$this->age = $age;
}
}
22. Constructor Argument Promotion (II)
After:
class User {
public function __construct(
private string $name,
private string $email,
private int $age
) {}
}