Re: [RFC] Safe Casting Functions
| From: | Lars Strojny | Date: | Mon, 20 Oct 2014 23:27:43 +0000 |
| Subject: | Re: [RFC] Safe Casting Functions | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-78163@lists.php.net to get a copy of this message | ||
Hi Andrea,
> On 21 Oct 2014, at 00:57, Andrea Faulds <ajf@ajf.me> wrote:
>
> Good evening,
>
> I am presenting a new RFC to add a set of three functions to do validated casts for scalar
> types:
>
> https://wiki.php.net/rfc/safe_cast
>
> Please read it.
I like the proposal except for one thing: the functions returning false in case of an error. As the
next logical function would be "to_bool()", I foresee a lot of trouble with regards to API
design as returning false there either means successful cast to false or an error while casting.
What about changing the functions to take an $isError variable as a second argument?
$value = to_string(1.2, $isError);
if ($isError) {
...
}
Alternatively one could do the same thing with an $isSuccess variable:
$value = to_string(1.2, $isSuccess);
if (!$isSuccess) {
...
}
Thoughts?
cu,
Lars