4 ms·
Does anyone know how the types in PHP7 compare to what's offered by Hack? It looks like generics aren't yet in the core language (https://wiki.php.net/rfc/gene
by lol768 10y ago
Does anyone know how the types in PHP7 compare to what's offered by Hack?
It looks like generics aren't yet in the core language (https://wiki.php.net/rfc/generics https://wiki.php.net/rfc/generics), but I believe return type hints work. I think we're also still missing property/field type hints (https://wiki.php.net/rfc/property_type_hints https://wiki.php.net/rfc/property_type_hints).
- bkanber 10y agoFWIW PhpStorm handles property type hints via docblock annotations and it works quite well, I've found myself not truly caring if the language enforces it as long as my IDE does.
- TazeTSchnitzel 10y agoPHP 7's type declarations are enforced at runtime, rather than Hack's compile-time types. This has its pros and cons. This means these declarations are always enforced, even when calling between typed and untyped code. However, there's no compiler to catch you out, at least not just from using the interpreter. Your IDE, or an external type checker (e.g. Phan) can check them for you, though. PHP 7 also gives you a choice between weak and strict type coercion modes, whereas Hack lets you choose between enforced type declarations and unenforced type declarations. As for the specific things you can type in PHP 7.0, there's parameter and return type declarations for `int`, `float`, `string`, `bool`, `array`, any class or method, and `callable` (a function name, method reference or closure). PHP 7.1 brings nullable types (prefix any type name with `?`), `iterable` (an array or an object implementing `Traversable`) and the `void` return type (enforces that a function doesn't use `return` with a value). Notable omissions are generics (even for arrays), and property types. There was a proposal for the latter, but it was rejected, for better or worse. As bkanber mentions, however, IDEs can still check property types if they're annotated using docblocks. Hack also has a few extra special types that PHP lacks.