Re: [RFC] Scalar Type Hinting With Casts (re-opening)
| From: | Jocelyn Fournier | Date: | Sun, 13 Jul 2014 13:13:33 +0000 |
| Subject: | Re: [RFC] Scalar Type Hinting With Casts (re-opening) | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-75394@lists.php.net to get a copy of this message | ||
Hi,
Le 13/07/2014 14:56, Andrea Faulds a écrit :
On 13 Jul 2014, at 13:50, Jocelyn Fournier <jocelyn.fournier@gmail.com> wrote:Actually displaying a warning in E_STRICT but still doing the implicit cast would not change the function signature.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.
Indeed :) But I have also performances in mind. Usually (at least in HACK / Zephir), "type hinting" is used to help the compiler optimize the code. If this type hinting could be useful to help my code working in a futur PHP->PECL compiler, or help the PHP opcache to do some optimisations, I'd like to use "type hinting" as much as possible. But with the above limitation, I'll not be able to do so. Thanks, JocelynI 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)) {You just demonstrated one: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 !function foo($bar) { if (!is_int($bar)) {If you really want to continue doing that, nothing would stop you.throw new Exception('Invalid parameter type');} }