Slides from my GTA-PHP Meetup talk about Hack which is the Facebook version of the PHP programming language which runs under their HHVM runtime environment for PHP. The focus of my talk was the language improvements that the Facebook team has added to PHP.
There's a lot of information in the presenter's notes, so if you're interested in Hack scroll down to see the extras.
React Native vs Ionic - The Best Mobile App Framework
An Elephant of a Different Colour: Hack
1. An Elephant of a Different Colour: Hack
Reduce your Server Load, Reuse your Code and
Recycle your PHP Skills with HHVM & Hack
Image Copyright Keith Evans. This work is licensed under the Creative Commons Attribution-Share Alike 2.0 Generic Licence. To view a copy of this licence,
visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
14. Map
•
Maps contain key / value Pairs
•
Keys can only be integers or strings
•
Values can be of any type
•
Order is preserved
15. Set
<?hh
$s = Set {'US', 'CA'};
$s->add('MX');
$s[] = 'NI';
$s[] = ($s->contains('CA') ? 'Has' : 'Lacks') . ' Canada';
print_r($s);
•
Can only contain integers or
strings
•
Unordered
HHSet Object
(
US
CA
MX
NI
Has Canada
)
16. Types
<?hh
function add(int $a, int $b): int {
return $a + $b;
}
var_dump(add(1, 2));
var_dump(add(1.5, 2));
int(3)
HipHop Fatal error: Argument 1 passed to add() must be an
instance of int, double given in
/home/vic/prog/presentation/type.php on line 4
19. Generics
<?hh
class Accumulator<T> {
function __construct(private T $value) {}
function add(T $value) { $this->value += $value; }
function get():T { return $this->value; }
}
$accumulateInts = new Accumulator<int>(5);
$accumulateInts->add(4);
var_dump($accumulateInts->get());
$accumulateFloats = new Accumulator<float>(0.7);
$accumulateFloats->add(1.6);
var_dump($accumulateFloats->get());
$accumulate = new Accumulator([5]);
$accumulate->add([4,9]);
var_dump($accumulate->get());
int(9)
float(2.3)
array(2) {
[0]=>int(5)
[1]=>int(9)
}
20. Generics
<?hh
class Accumulator<T> {
function __construct(private T $value) {}
function add(T $value) { $this->value += $value; }
function get():T { return $this->value; }
}
$accumulateInts = new Accumulator<int>(5);
$accumulateInts->add(4);
$accumulateInts->add(4.4);
var_dump($accumulateInts->get());
float(13.4
)
!
21. Nullable Types
<?hh
function printNum(?int $num) {
echo "num is ";
echo is_null($num) ? "null" : $num;
echo "n";
num is 1
}
num is null
printNum(1);
HipHop Warning: Argument 1 to
printNum(null);
printNum() must be of type ?int,
printNum("Five");
string given in
/home/vic/prog/presentation/nullabl
e.php on line 6
num is Five
22. Types
<?hh
class MyClass<T as SomeInterface> {
function __construct(private T $v) {}
}
newtype Secret as BaseClass =
MyClass;
27. User Attributes
•
You can add attributes / annotations to classes,
methods, functions and arguments.
•
You can specify more than one set of user
attributes:
<< Foo(1), Bar(2) >>
•
You can’t use constants or variables in attributes
28. Async
•
I lied; hhvm’s async is more c# than node.
•
Library functions like evhttp_async_get() don’t
return Awaitable’s
•
•
So they don’t seem to play well together
PocketRent/beatbox uses async, but I don’t grock it
29. class Foo{}
Async
class Bar {
public function getFoo(): Foo {
return new Foo();
}
}
This is the example
Facebook provided
on GitHub for async.
async function gen_foo(int $a): Awaitable<?Foo> {
if ($a === 0) {
return null;
}
$bar = await gen_bar($a);
if ($bar !== null) {
return $bar->getFoo();
}
}
return null;
async function gen_bar(int $a): Awaitable<?Bar> {
if ($a === 0) {
return null;
}
}
return new Bar();
gen_foo(4);
30. More
•
See hhvm.com
•
The debugger is command line, and very good:
hhvm -m debug
•
Sara Golemon is giving a talk tomorrow night about HHVM and
hack, and it will be live streamed:
http://www.meetup.com/sf-php/events/159404382/
•
Framework for Hack:
https://wiki.pocketrent.com/beatbox/start
•
I promise to blog about Hack at http://blog.vicmetcalfe.com/
Notas del editor
Ask audience what they think each photo represents.
Some of the HHVM Team: Comes from Facebook, Facebook runs on HHVM.
HHVM’s primary goal is speed, and it is very fast
Silk Street in Beijing, famous for knock-off fasions. HHVM closely duplicates Zend’s PHP interpreter.
Evolution of technology. HHVM’s Hack takes PHP language to the next level.
Require init.php
{$x} notation for substitution
Escapes values
Allows expressions including function calls, etc
No quotes for attributes
No substitution without {}
Special &lt;x:doctype&gt; element
Cool, we can use [] to -&gt;add()
Cool, we can remove items with removeKey()
Boo, we can’t remove items with unset()
Vector re-indexing on removeKey vs no re-indexing on Array.
I prefer to know that an array is zero based or not based on its type
Pairs can contain any two items
XHP is interesting (and out of place) here
Yes! You can use [] to add a Pair as shorthand for -&gt;add()
Type hints for primitive types!
Types are (sometimes) enforced!
Shapes are like typed arrays which specify legal keys
Return shape() returns an array
Shapes map to arrays, not to Maps which are Map objects.
Wait for someone to say “Can’t you typedef?” before next slide
Hurray typedef!
Extra keys are ignored
Missing keys DON’T CAUSE AN ERROR until you try to use the key
Any advantage to shapes over arrays? I don’t know. Hopefully shape mismatches can be caught in the future as type errors.
Constructor shorthand
The rest pretty well speaks for itself
Type information doesn’t seem to be used at runtime. Debugger doesn’t show type of value.
Rewriting without generics or type info yields same behaviour.
Is there a point to having generics if they don’t enforce types?
My tests didn’t show improved speed with generics over typeless either.
Hopefully these before enforced down the road
Appears to be partially implemented / future features here.
as should probably constrain types to a given superclass or interface, but currently appear to do nothing
newtype will hide type information outside of the file it is declared in according to Joel Marcey (https://github.com/facebook/hhvm/issues/1846)
Review: Plain old PHP anonymous functions
No function keyword, no return statement, no curly brackets!
Curly brackets and return statement are optional
If only one parameter brackets are optional
No need for use clauses!
Access to variables in scope are preserved
Node minimizes CPU context switching to achieve high performance
hhvm still uses threading extensively and is not ‘evented’
Node minimizes CPU context switching to achieve high performance
hhvm still uses threading extensively and is not ‘evented’