(When) should I use type hinting in PHP?

前端 未结 6 1982
独厮守ぢ
独厮守ぢ 2020-12-05 23:42

I can\'t understand the motivation of PHP authors to add the type hinting. I happily lived before it appeared. Then, as it was added to PHP 5, I started specifying types eve

6条回答
  •  一个人的身影
    2020-12-06 00:26

    It's not about static vs dynamic typing, php is still dynamic. It's about contracts for interfaces. If you know a function requires an array as one of its parameters, force it right there in the function definition. I prefer to fail fast, rather than erroring later down in the function.

    (Also note that you cant specify type hinting for bool, int, string, float, which makes sense in a dynamic context.)

提交回复
热议问题