php's type hints are completely broken.

Why is strict mode not the default? Why does it completely break down for arrays? (You have to abuse phpdocs to get any meaningful hints but you still lose any runtime checks.) What's with union types? (I know, php8 now has them but what took you so long.)

  • 2
    It all comes down to compatibility with more existing code running in production than you or i will ever see in our entire lifes combined.
    That is the joy of maintaining a language with a history.
    Just look at the hate, Python received for correcting their mistakes of the past when going for major version 3...

    Apart from that, PHP is a dynamically weakly typed language.
    The language is not designed with a modern type system in mind.
    So its type system really can't ever become more than a pig coated with some fancy makeup. It might be surprisingly pretty for a pig, but it will still be a pig...

    That said i still hope for pseudotypes like list[elementtype] (non-sparse numerically indexed array with keys starting at zero) and tuple[type1, type2,... typeN] (list of fixed length and with a type hint for each element).
  • 0
    Honestly it sounds like you are using an array where you should be using an object. Type hints are a great improvement that help us with better interfaces and catch many mistakes that are common in dynamic typed languages. It gets better every version of PHP. In 7.0 you had to use an exception because you can't return null and define a return type.
    Arrays and stdClass are great for dynamic structures like incoming JSON data. In a language like Go you constantly have to cast or coerce need to put everything in structs both normal and paged results. I have to write 4 times as much code to handle the Spotify API in Go than in PHP because of that.
Add Comment