3 ms·
Since you are working with dynamic language, step 2 should include adding type annotations or whatever they are called in php. Specifying stricter types makes r
by Too 9y ago
Since you are working with dynamic language, step 2 should include adding type annotations or whatever they are called in php. Specifying stricter types makes refactoring much easier.
- maxxxxx 9y agoAgreed. Adding type annotations is already a huge refactor in itself though and pretty risky with PHP. Once you have that done life will be much easier.
- mdaniel 9y agoFor those unfamiliar with php, or its type annotations, why is adding type annotations a risky step? Do they become enforcing? function foo(string $bar) { ... } foo(1234) // kaboom?
- maxxxxx 9y agoI think your code will work because 1234 can be converted to a string. I think the trouble starts once you pass around objects.
- w0rd-driven 9y agoIt actually doesn't according to http://sandbox.onlinephpfunctions.com/code/8804b49c23d62022854e928ef433c0ab9a5cbc5f http://sandbox.onlinephpfunctions.com/code/8804b49c23d620228..., though different PHP versions could produce different results.
- maxxxxx 9y agoThis works with PHP 7 <?php //Enter your code here, enjoy! function foo(string $bar) { return $bar . $bar; } echo foo(1234);
- w0rd-driven 9y agoYeah pretty much. It's risky because type checking produces fatal errors that aren't always easy to trap properly. You don't realize how much a code base relies on PHPs type coercion until you add annotations and watch it blow up, in often subtle ways.
- maxxxxx 9y ago"Subtle" is the keyword here.
- Too 9y agoWait? You mean you get runtime errors? That's well...good in a way but as you say dangerous on old code. To get the true benefits of typing you should also run a static type analyzer.
- maxxxxx 9y agoAre there any for PHP?