Re: [RFC] Scalar Type Hinting With Casts (re-opening)
| From: | Andrea Faulds | Date: | Sun, 13 Jul 2014 12:56:08 +0000 |
| Subject: | Re: [RFC] Scalar Type Hinting With Casts (re-opening) | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-75393@lists.php.net to get a copy of this message | ||
On 13 Jul 2014, at 13:50, Jocelyn Fournier <jocelyn.fournier@gmail.com> wrote:
> Would it possible to control the implicit casting through a PHP variable ? (or perhaps modify
> E_STRICT to display a warning if this kind of implicit cast is done ?)
No. I mean, yes, it’s possible, but allowing the effective function signature of a function to
change based on some variable or ini setting does not sound like a good idea to me.
> I mean specifying explicitly a type is often useful to make sure the proper type is passed to
> the function, and there's no bug in the calling func.
>
> e.g. safety check like
>
> function foo($bar) {
> if (!is_int($bar)) {
> throw new Exception('Invalid parameter type');
> }
> }
>
> will not be "equivalent" to
>
> function foo(int $bar) {
>
> }
>
> and there will be no way to have an equivalent behaviour once "type hinting" is used
> !
You just demonstrated one:
> function foo($bar) {
> if (!is_int($bar)) {
> throw new Exception('Invalid parameter type');
> }
> }
If you really want to continue doing that, nothing would stop you.
--
Andrea Faulds
http://ajf.me/