On 15/07/2014 20:44, Andrea Faulds wrote:
PHP has pseudo-functions, maybe we could use that format? cast_int($foo)?

Actually, perhaps the solution is an ahead-of-time check:

     if (safe_cast($foo, int)) {
         $bar = (int!)$foo;
     } else {
         $bar = 1;
     }

I think this would be quite nice - keeps things relatively short without introducing so much punctuation that it starts looking like a Perl script ;)

The only problem is that either you need a whole set of functions (safe_int($foo), safe_float($foo), etc) or you need some format for that second argument, which gets you into some odd meta-validation: if you take a string, or an integer (expected to be in the form of a constant like PHP_INT), then it would be possible to write safe_cast($foo, $bar), and now we need rules on validating $bar, and what happens if it's wrong.

The magic form with a keyword like you've written avoids that, but it feels kind of weird - like when SQL date functions take things like "day" and "hour" as barewords rather than strings.

Maybe I'm overthinking it though.

--
Rowan Collins
[IMSoP]


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to