4 ms·
I'm still not so convinced by PHP's runtime type checking system. It adds a little overhead, I guess that's not such a big problem, but it adds also new classes
by naranha 7y ago
I'm still not so convinced by PHP's runtime type checking system. It adds a little overhead, I guess that's not such a big problem, but it adds also new classes of runtime exceptions that can only be caught using tests, or with a good static analyzer. Especially if you now upgrade your application to 7.4 you can only do it with confidence when you have 100% test coverage.
If a static analyzer can detect these mistakes, why even add runtime checks for it? Think TypeScript/Javascript, after the compilation is done, most type checks are removed for performance reasons.
Or perhaps there is something I don't see...
- beberlei 7y agoIf you upgrade your application to 7.4, all the code doesn't have typed properties. Only when you then add them after the upgrade you should have tests for that or a static analyzer that tells you the changes you make are correct.
- naranha 7y agoYes that's true - it's a backwards compatible change. Still, static analysis is necessary when you upgrade dependencies and for your own code. For example for seeing whether adding a type to a property breaks code somewhere else. Discovering type errors at runtime is often too late. Luckily there are good static analyzers for PHP.
- donatj 7y agoI’m not sure about the properties but on method parameters at least, they will cast, e.g. if you give foo(int $bar) the string “10” it will be cast to 10. It will runtime error only if it cannot “safely” cast the value. I’m not 100% that it is a wonderful behavior, and a lot of people turn it off (which can be done on a per file declaration of strict) but for us it’s genuinely helped eliminate an entire class of error where a runaway type error would result in a really whacky value way down the line, even occasionally ending up with that stored in the database for some calculation. The kind of thing we still end up with in JavaScript to this day.
- TazeTSchnitzel 7y agoThe types being enforced at runtime can be seen as a last resort. It means static checkers can actually rely on the types being accurate as code which breaks the type contract won't run.