RE: [PHP-DEV] Scalar Type Hints v0.4
| From: | François Laupretre | Date: | Wed, 18 Feb 2015 17:34:49 +0000 |
| Subject: | RE: [PHP-DEV] Scalar Type Hints v0.4 | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-83096@lists.php.net to get a copy of this message | ||
> De : Patrick ALLAERT [mailto:patrickallaert@php.net]
> ini_set("coercion_reporting", COERCION_ERROR); // Fail in case of
> potentially bad coercion
>
> foo(7);
> // int(7)
> foo("7");
> // int(7)
> foo("7 dogs");
> // Catchable fatal error: Unsafe coercion transforming "7 dogs" to "7".
>
> The biggest advantage, IMHO, is that you get the exact same result whether
> you do:
>
> foo((int) $value);
>
> or:
>
> foo($value);
>
> ... whatever the mode you are in.
>
> Basically, this is weak type hints + something similar to what I
> contributed in the past with the "Array to string conversion" notice (see:
> https://github.com/php/php-src/commit/d81ea16e).
>
> Care to share the pro's/con's you see with a solution like that?
That's a good idea, IMO. We can add an optional message when a conversion is executed in ZPP
macros. It can help. The main drawback is that it is pure runtime check, not suitable for static
analysis and related tools.
Regards
François