# make casts more strict in PHP 7

- Thread: https://externals.io/message/77117
- Original archive: https://news-web.php.net/php.internals/77117
- Started by Robert Stoll on 2014-09-09T19:26:07+00:00
- Messages: 10
- RFC: Add scalar type hinting with casts (Withdrawn) — https://externals.io/rfc/scalar_type_hinting_with_cast.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #77117 — Robert Stoll — 2014-09-09T19:26:07+00:00

URL: https://externals.io/message/77117#77117

Heya,

I remember that some people were complaining about the inconsistencies
between normal conversions and the one applied in Andrea's RFC about "Scalar
Type Hinting With Casts". 
https://wiki.php.net/rfc/scalar_type_hinting_with_cast

As an example:

$i = (int) "a"; //fine without errors
function foo(int $i){}
foo("a"); //E_RECOVERABLE_ERROR

Personally, I do not like such inconsistencies either but I like that the
conversion rules in Andrea's RFC are more strict. And thus I was asking
myself if it would not make sense to change the current behaviour of
castings and make them more strict in PHP 7.
In the same time we could introduce a cast operator as the "as" operator in
C# which would have the current behaviour.
I believe that we would make PHP applications more robust in this way. At
least I doubt that the majority wants to convert "a" to 0. IMO most people
would prefer that PHP bails out an error in such a case. Sure one could also
write if's and check first if it is numeric etc. but that should be easier
IMO.

Just as an idea. What do you think?

Cheers,
Robert

---

## #77119 — Andrea Faulds — 2014-09-09T20:19:21+00:00

URL: https://externals.io/message/77117#77119 · In reply to: #77117

> Personally, I do not like such inconsistencies either but I like that the
> conversion rules in Andrea's RFC are more strict. And thus I was asking
> myself if it would not make sense to change the current behaviour of
> castings and make them more strict in PHP 7.

No, no it would not. PHP’s explicit casts cannot fail, and there is absolutely no good reason to change this. If people want strict casting, we can add new functions or operators for that specifically. But to break explicit casts and make them sometimes fail would cause innumerable bugs and backwards-compatibility ideas.

--
Andrea Faulds
http://ajf.me/

---

## #77123 — Stas Malyshev — 2014-09-09T23:30:50+00:00

URL: https://externals.io/message/77117#77123 · In reply to: #77119

Hi!

> No, no it would not. PHP’s explicit casts cannot fail, and there is
> absolutely no good reason to change this. If people want strict
> casting, we can add new functions or operators for that specifically.
> But to break explicit casts and make them sometimes fail would cause
> innumerable bugs and backwards-compatibility ideas.

I agree. If the developer explicitly states they want conversion, it
should be conversion. For implicit casts, some tightening may be ok, but
for explicit ones I think it would bring much more headaches than it's
worth.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/

---

## #77127 — Mike Willbanks — 2014-09-10T02:01:45+00:00

URL: https://externals.io/message/77117#77127 · In reply to: #77123

> Hi!
>
> > […]
>
> I agree. If the developer explicitly states they want conversion, it
> should be conversion. For implicit casts, some tightening may be ok, but
> for explicit ones I think it would bring much more headaches than it's
> worth.
>

Precisely, it would cause a vast amount of headaches for many projects.
 Think of user input where you are expecting an integer and it must be a
positive integer (which would ultimately be a string); it is much faster to
do a quick check such as:

$foo = (int) $_GET['foo'];
if ($foo > 0) {
    ....
}

I'd personally not have to wrap more items around it due to a notice or
warning.

---

## #77138 — Rowan Collins — 2014-09-10T17:41:55+00:00

URL: https://externals.io/message/77117#77138 · In reply to: #77119

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"...

-- 
Rowan Collins
[IMSoP]

---

## #77139 — Michael Wallner — 2014-09-10T18:27:39+00:00

URL: https://externals.io/message/77117#77139 · In reply to: #77138

> Andrea Faulds wrote (on 09/09/2014):
> > […]
> 
> 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

---

## #77140 — Rowan Collins — 2014-09-10T19:36:55+00:00

URL: https://externals.io/message/77117#77140 · In reply to: #77139

Michael Wallner wrote (on 10/09/2014):
> .oO(ext/filter) ?

Maybe I'm just missing some better documentation, but my main problems 
with ext/filter are:

1) Many users don't know it exists, or how to use it.
2) It's pretty verbose and complex for such a simple task as "is this 
thing a valid integer?"

Basically, it's a swiss army knife, but people are looking for a pair of 
scissors.

There's a lot of code out there doing things like this:

if ( is_numeric($_GET['user_id']) ) {
     display_user($_GET['user_id']);
}

This is definitely the wrong function to use (what kind of a user ID is 
'-1.5e-50'?) but it *looks* like the right function, and it has a really 
simple name, so people don't bother looking for anything more complicated...

-- 
Rowan Collins
[IMSoP]

---

## #77145 — Andrea Faulds — 2014-09-11T02:03:34+00:00

URL: https://externals.io/message/77117#77145 · In reply to: #77138

> 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"…

You know, “int”, “float”, “string” etc. aren’t currently reserved words. We could add functions, or even make them reserved words and add internal functions (à la isset), so int(‘2’), float(‘2.0’), string(‘test’) etc.

Or perhaps class-like constructors? $a = new int('2'); ? Would kinda make sense given some people want to add methods to primitive types.

--
Andrea Faulds
http://ajf.me/

---

## #77147 — Park Framework — 2014-09-11T03:02:43+00:00

URL: https://externals.io/message/77117#77147 · In reply to: #77145

> You know, “int”, “float”, “string” etc. aren’t currently reserved words. We could add functions, or even make them reserved words and add internal functions (à la isset), so int(‘2’), float(‘2.0’), string(‘test’) etc.
>
> Or perhaps class-like constructors? $a = new int('2'); ? Would kinda make sense given some people want to add methods to primitive types.

+1

$int     instanceof Int
$float  instanceof Float
$num  instanceof Numeric
$bool  instanceof Boolean
$str     instanceof String
$array instanceof Array

This makes it possible not to use the is_*() functions.
It is desirable to conduct instanceof fully consistent with behavior
hinting casts, it would be logical and correct.
Just wanted to be able to use these classes (or interfaces) to create
their objects, these objects would pass inspection in hinting casts.

---

## #77151 — Rowan Collins — 2014-09-11T22:00:13+00:00

URL: https://externals.io/message/77117#77151 · In reply to: #77147

> > […]
>words.

Well, they are in the context of casts:

$a = (int)'42';
$a = (string)42;
etc

> We could add functions, or even make them reserved words and add
>internal functions (à la isset), so int(‘2’), float(‘2.0’),
>string(‘test’) etc.

How would this be different from what we already have? For the function version (useful as a callback), we have `intval()`, `strval()`, etc

> > […]
>make sense given some people want to add methods to primitive types.
>
>+1
>
>$int     instanceof Int
>$float  instanceof Float
>$num  instanceof Numeric
>$bool  instanceof Boolean
>$str     instanceof String
>$array instanceof Array
>
>This makes it possible not to use the is_*() functions.
>It is desirable to conduct instanceof fully consistent with behavior
>hinting casts, it would be logical and correct.
>Just wanted to be able to use these classes (or interfaces) to create
>their objects, these objects would pass inspection in hinting casts.

This could cause a lot of confusion. If these "constructors" are just a new syntax for type casting, there's no obvious reason to behave differently in terms of strictness, which is what we were discussing previously. Worse, they would look like objects, but behave as plain variables (no reference-like ability to modify the value in place).

Alternatively, these could be special "boxed types", which were real objects acting almost but not quite the same way as the equivalent basic type. Some languages (e.g. Java) have those, but they're frankly rather confusing, and I'm not sure what the benefit would be.

Regards,
-- 
Rowan Collins
[IMSoP]
