r/lolphp • • Nov 15 '14

new safe casting function RFC. casting "-10" to string is valid but casting "+10" is not..

here the comment where one user asked the author of RFC about this. I am not able to follow his reasoning. What do you think?

21 Upvotes

67 comments sorted by

View all comments

Show parent comment

-2

u/[deleted] Nov 15 '14

it also breaks PHP's own shitty conventions

Well, yes, that is rather the entire point of these functions.

1

u/[deleted] Nov 15 '14

So if someone entered a phone number, e.g +xxxxx, you'll treat that as not being an integer.

And if someone was writing a mathematical or scientific software in which people would enter +/- signs, you'll treat the positive numbers as strings.

You're not making things better, by treating mathematically accurate numbers as strings, you're actually being an even bigger dumbass than the people who did null == false.

2

u/[deleted] Nov 15 '14

So if someone entered a phone number, e.g +xxxxx, you'll treat that as not being an integer.

You really, really shouldn't be handling phone numbers as integers. For starters, the + in a phone number is significant and changes the meaning of it. Phone numbers often have infixed - or spaces. Phone numbers can have significant leading zeros. Phone numbers won't necessarily fit in a 32-bit integer. Phone numbers can't be operated on like normal numeric values. Phone numbers can contain brackets. Phone numbers can contain # and *. Phone numbers... well, you get the picture.

And if someone was writing a mathematical or scientific software in which people would enter +/- signs, you'll treat the positive numbers as strings.

Er, no, this specific function would reject the +, but you could always strip it off yourself as it's completely redundant, e.g. to_int(ltrim($value, '+')).

7

u/[deleted] Nov 15 '14

you could always strip it off yourself

So you make a programmer do extra work to make up for your idiotic decision to treat mathematically valid integers as strings.

Anyway, I'm not sure why I'm trying to convince you. I actually take great pleasure in watching the train wreck which is PHP, and seeing it get more and more convoluted is quite enjoyable. Please, do carry on, and add another 5-6 cases where it breaks existing conventions and mathematical rules. E.g, may be have 12 == 'december' return true. It would be awesome.

You're doing god's work. Keep it up.