The case against static typing in PHP (2023)
tonymarston.net
tonymarston.net
Years back, as a beginning programmer I got really confused by his other articles. As I was looking for guidance on application design I took him seriously but also noticed that his examples of "good design" were in fact the opposite.
Later it dawned upon me that the was doing COBOL in PHP. Those lengthy articles are mostly bad takes, but it is interesting nonetheless to see how strict the last believer sticks to the Old Belief. I would take none of his advice, but still have big respect for his stamina.
Old common static typing languages felt bad and restrictive and contorting just because typing is hard that when people created them made a relatively simple, underpowered thing which was as complex as they could make. Understanding of types advanced much since then so now you can conveniently and partially add types even to a dynamic language with rich semantics and there's very little reason not to as (unitentionally) demonstrated by this article.
I've seen too many bugs with PHP static typing for it to be worth it in mature systems. It's great in greenfield, but yeah, old code is hard to work it into unlike typescript which tends to be more forgiving. Though, I will say, typescript is just as viral if not more so.
Can you elaborate? What kind of bugs? Can you give some examples? I'm scratching my head because I'm not sure if I ever experienced such a bug.
"Have you ever changed the type of a function argument and then had to find all usages and change them? Have you ever changed a class and then had to recompile, and redeploy, all the classes that used that class? These are just three of the issues that don't occur nearly as often in dynamically typed languages."
If you change the type of an argument you OF COURSE want/need to change all usages of that function in both static and dynamic languages - how in the world did you expect sqrt(struct Person) to behave? - but static typing helps you quickly find all those instances so you're more productive in making that refactor.
"Programmers thinking that dynamic typing is better should never use a dynamically typed language, whereas programmers thinking that static typing is the only sane choice may use a dynamically typed language in special cases."
People that don´t see dangers take more risks, but they are also the least likely to understand your advice. People that don't understand how types help to adapt and grow software are the least likely to refactor when needed to.That is why I think it is fine to use a type-brittle programming language for quick scripts, but not for building applications that have to change over time and need to be maintained by multiple people.