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?

22 Upvotes

67 comments sorted by

View all comments

Show parent comment

8

u/i_make_snow_flakes Nov 15 '14

This isn't really suited for that use case.

Well, what is the intended use case then..Can you give some examples?

-7

u/[deleted] Nov 15 '14

Say you have a site where you have a user profile page that looks up a user by ID:

http://example.com/user.php?id=12

<?php
User::get(to_int($_GET['id']));

In this scenario, it doesn't make sense to allow IDs like %20+012.0%20.

7

u/i_make_snow_flakes Nov 15 '14

I am not sure I see anything wrong allowing + signs..Even if there was, I am not sure a casting function behavior should be limited or tailored to filtering input values...

-7

u/[deleted] Nov 15 '14

limited or tailored to filtering input values

What?

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.

→ More replies

0

u/[deleted] Nov 16 '14

[deleted] — view deleted comment

1

u/[deleted] Nov 16 '14

Isn't + a space according to url encoding standards?

Yes, that's true. I couldn't be bothered looking up what + encodes to, sorry.

Or do you mean an example like %20%2B012.0%20?

Yes.