Every once in a while the flexibility of dynamic types comes back and bites you in the ars:

So I created method that returns the date significance (day, month, year) or null when no date is set.
I chose a class constant DAY with the value 0.
This is not a problem in if statements as I always use === but in this case a switch made more sense. And as you can guess no date set (NULL) was handled in the self::DAY (0) case... 😐😑😶 Silently resulting in wrong results when no date is given! #£#@$& (and other comic swearing symbols)

Even though php7 finally has decent type hinting resulting in much clearer defined API's we can still go very wrong.
More love to Go for less verbose static typing! To bad we can rarely use it at the office 😥

  • 2
    I feel you.

    The switch statement by definition uses loose comparison, so this is expected behavior. Well, expected for php ;)

    So you might run into this troubles also with strings and int comparision, as

    `(2 == '2string') === true)`

    This is also why I try to reduce the places where I inject nullable parameters into functions and make sure that I have a variable of a certain type via guards and early returns.
  • 2
    Yep somewhere back in my mind I knew that switch was doing lose comparison. As you noted logical for php is not really all that desired, Python 3 does a better job here.

    I have modified my code to return self:NONE made that 0 and numbered up from there. Guess that is more or less what you always do in the first place. wk41 Is as easy as trying to learn from your mistakes and look at how others solve it.
Add Comment