4 ms·
There are a lot of great things here but PHP just needs to embrace static typing (IMHO). Take match expressions plus enums. Incredibly useful. You know what's
by cletus 5y ago
There are a lot of great things here but PHP just needs to embrace static typing (IMHO).
Take match expressions plus enums. Incredibly useful. You know what's even more useful? When it's a type error to leave out one of the values. This means when you add a new value you'll get a bunch of errors you need to fix. Technically you can get around this with a default case but I consider that an anti-pattern in most such cases.
The Hack type system, particularly the ability to express nullability, is just vastly superior.
Named parameters are an interesting one. The Hack equivalent is to use shapes [1]. This is slightly verbose but you can express what shape fields are required and optional. Javascript manages without named parameters using anonymous objects, which is basically the same thing.
I didn't know PHP lambda expressions can't be multi-line. That seems weird. Again, Hack is simply better here allowing both this syntax and anonymous functions, all of which can be multi-line.
I also find the Hack collection types (vec, keyset, dict) to be superior to PHP's array with more obvious semantics and less weird type coercion. PHP's default of maintaining insertion order is amazingly useful and I'm honestly surprised more languages don't copy this. Of course you have things like LInkedHashMap in Java but doing this out-of-the-box I tend to find more natural.
Anyway, I think the hate on PHP is a trope at this point, largely outdated and just boring. Modern PHP is actually quite pleasant to develop in (IMHO).
[1]: https://docs.hhvm.com/hack/built-in-types/shape https://docs.hhvm.com/hack/built-in-types/shape
- yurishimo 5y agoI don’t think we need static typing as a requirement. For folks who want to opt in, it’s probably fine. The divide could be catastrophic and for me, would be a major turn off. Look at JS and TS. Some projects go all in, but then others half ass it with `any` return types all over the place. If the JS ecosystem can come together around TS, it’s probably still a decade out until it becomes the default used everywhere. That gap creates confusion and chaos. If people want similar languages with more/less features, they can create those communities. Erlang and Elixir are good examples.
- cletus 5y agoI don't think there's an alternative to optional static type checking at this point. Like you say, there's a bunch of untyped code out there. I'd actually hate to see a JS/TS divide in PHP. The typing needs to really be a superset of existing PHP and backwards compatible. Such a typechecker should cover at least the following: 1. Point it to a codebase and spit out a bunch of errors/warnings; and 2. Run it at runtime where type checks are logged or a strict mode where they're fatal.
- throw_m239339 5y ago> There are a lot of great things here but PHP just needs to embrace static typing (IMHO). Static typing cannot be retrofited into PHP. Core already has hard time with generics. PHP Types will always be just type guards with dynamically typed variables.
- muglug 5y ago> The Hack type system, particularly the ability to express nullability, is just vastly superior. I agree the type system is superior, given the language was created to have a type system in the first place (PHP was not). But PHP has allowed nullable types in syntax since PHP 7.1. > Named parameters are an interesting one. The Hack equivalent is to use shapes Hack shapes are not Hack’s answer to named parameters. Instead they’re a way to specify dict contents on a more granular level. A comparable array shape type, expressed in docblock form, is now supported by all major PHP static analysis software. > Take match expressions plus enums. Incredibly useful. You know what's even more useful? When it's a type error to leave out one of the values. Most PHP static analysis tools give you this information. > I also find the Hack collection types (vec, keyset, dict) to be superior to PHP's array with more obvious semantics and less weird type coercion. Absolutely agree with this, but for now (and maybe in perpetuity) we’ll have to settle for the swiss-army-knife array type.
- mekster 5y ago> Javascript manages without named parameters using anonymous objects, which is basically the same thing. It's not. Unless you have a strict typing on the function in TS, you can't know if you're typing the right member. This is actually one of the thing in PHP I like more than TS. It takes no effort to provide meaningful names to parameters from the caller.