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.
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.
$x = [1,null,'xxxx', collect([])];
gettype($x) is "array". you can annotate a type as being an `array`. How does that help me exactly?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).
There's a current RFC that is aiming to deprecate this 'feature'.