Re: [PHP-DEV] make casts more strict in PHP 7

[email protected] (Michael Wallner)
Newsgroups php.internals
Message-ID <[email protected]>
On 10/09/14 19:41, Rowan Collins wrote:
> Andrea Faulds wrote (on 09/09/2014):
>> If people want strict casting, we can add new functions or operators
>> for that specifically.
> 
> I think this came up during the previous discussion, and I actually
> think it would be quite nice to have some functions both to perform a
> "strict"/"safe" cast (and raise E_CATCHABLE_ERROR or perhaps throw
> TypeCastException), and to check if a variable *can* be thus cast
> (validating integers is unnecessarily complicated at the moment).
> 
> Either:
> mixed safe_cast(mixed $var, string $type)
> boolean can_safe_cast(mixed $var, string $type)
> e.g.
> if ( can_safe_cast($_GET['i'], 'int') )
> $i = safe_cast($_GET['i'], 'int');
> 
> Or, to avoid representing the type with a string arg, a whole set of
> functions:
> int safe_cast_int(mixed $var)
> boolean can_safe_cast_int(mixed $var)
> float safe_cast_float(mixed $var)
> boolean can_safe_cast_float(mixed $var)
> etc
> 
> The first avoids creating so many new functions, but perhaps some
> constants should be defined for the argument, based on the values
> returned by gettype():
> 
> PHP_TYPE_INT = "integer"
> PHP_TYPE_FLOAT = "double"
> PHP_TYPE_STRING = "string"
> PHP_TYPE_ARRAY = "array"
> PHP_TYPE_OBJECT = "object"
> PHP_TYPE_RESOURCE = "resource"
> PHP_TYPE_NULL = "NULL"
> 
> 
> Of course, we'd then need to agree exactly which casts are considered
> "safe"...
> 


.oO(ext/filter) ?

-- 
Regards,
Mike
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.