Hacker News new | ask | show | jobs
by perttir 1680 days ago
Php has developed a lot in a decade.

Lots of new good things in php7/php8. The typecasting is way better than before, but it still allows you to be more "dynamic" if you want to.

1 comments

   $x = [1,null,'xxxx', collect([])];
gettype($x) is "array". you can annotate a type as being an `array`. How does that help me exactly?
Since $x is an array, it will get rejected at runtime by `function f(int $a)`. So this `array` type is limited but useful.

You can also add annotations like `/* @var []int */`. External tools (psalm, phpstan) use annotations in their static analysis of the code and will raise an error if $x elements are not integers.

Of course, it's far from Haskell, but my experience with types in PHP is smoother than in Python. Though it was 2 years ago in Python, and the environnement has probably matured.

In my opinion, a worse problem with PHP is that classes properties are dynamic. `class A {}; $a=new A; $a->x=1;` is perfectly valid and will add a property to the object that does not exist in the class. There's no simple way to forbid this, even at runtime (hacking the magic `__set()` creates other pain points).

> In my opinion, a worse problem with PHP is that classes properties are dynamic.

There's a current RFC that is aiming to deprecate this 'feature'.

https://wiki.php.net/rfc/deprecate_dynamic_properties