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

6

u/i_make_snow_flakes Nov 15 '14

Just that I think that is not a valid use-case for a casting function...

-5

u/[deleted] Nov 15 '14

It's not filtering, it's casting with a measure of validation. This is needed because PHP's current explicit casts ((int) etc.) never fail, which is quite dangerous and you can mangle input with them.

6

u/i_make_snow_flakes Nov 15 '14

it's casting with a measure of validation

There. This is what is wrong. It is actually two things but the name says only one, which is casting. And you end up having to implement this weird behavior with respect to the first half (which the name implies), to support the other half of the functionality.

And you seem to be down voting me before replying. really?

-2

u/[deleted] Nov 15 '14

There. This is what is wrong. It is actually two things but the name says only one, which is casting.

Uh, many languages have casting functions which can fail.

4

u/i_make_snow_flakes Nov 15 '14

They fail when the value cannot be casted. Not when it does not fit the expected format...

-1

u/[deleted] Nov 15 '14

That's the same thing.

3

u/i_make_snow_flakes Nov 15 '14

No, it's not.

-2

u/[deleted] Nov 15 '14

OK, elaborate on what the difference is, then. Fundamentally, no value can't be casted somehow. If something "can't be casted", that means they've chosen not to support a specific type or format of input, or the reverse, chosen only to support a specific type or format.

4

u/i_make_snow_flakes Nov 15 '14

Simple question: is +1 a valid integer in PHP? is the code $a = +1; valid? and will $a have value int(1) after this statement?

And are you really down voting me before replying?

1

u/midir Nov 16 '14 edited Nov 16 '14

+1 parses as a valid integer, but the + is not part of the integer literal; it is a unary plus operator attached to the integer literal. Note the difference here when there is arbitrary whitespace between the + operator and number:

var_dump(+ 1); // int(1)
var_dump((int)'+ 1'); // int(0)

The difference is because whitespace is allowed in the source, but not by the string-to-int conversion, and other languages are the same in that regard.

Likewise:

var_dump(+-+-+1); // int(1)
var_dump((int)'+-+-+1'); // int(0)

var_dump(+(1)); // int(1)
var_dump((int)'+(1)'); // int(0)

My point is that merely because +1 is valid source code is not in itself a reason why it should be accepted by a general string-to-int conversion. Anther difference between source parsing and general string-to-int conversion is the treatment of leading zeros: being treated as octal in source but as decimal by string-to-int conversion.

→ More replies

-1

u/[deleted] Nov 15 '14

Simple question: is +1 a valid integer in PHP?

No. But technically -1 isn't either. Like most languages, PHP doesn't have signs in its numeric literals.

and will $a have value int(1) after this statement?

Yes, because the unary plus (+) does nothing.

→ More replies