Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75392 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24524 invoked from network); 13 Jul 2014 12:50:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jul 2014 12:50:17 -0000 Authentication-Results: pb1.pair.com header.from=jocelyn.fournier@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jocelyn.fournier@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.180 as permitted sender) X-PHP-List-Original-Sender: jocelyn.fournier@gmail.com X-Host-Fingerprint: 74.125.82.180 mail-we0-f180.google.com Received: from [74.125.82.180] ([74.125.82.180:51303] helo=mail-we0-f180.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B2/31-16748-78082C35 for ; Sun, 13 Jul 2014 08:50:16 -0400 Received: by mail-we0-f180.google.com with SMTP id k48so2061244wev.11 for ; Sun, 13 Jul 2014 05:50:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=Y87EFPQni8pXWlT2SnmWvlKmgnvAi7kcM6rXGqhpG80=; b=OfiSgdLPM58/GUk7R2nAF4yCXhIs+S3qN5clGt3YPaneyrWw1rFCRZ0UCNkjH7XQVF vCVp6w8EKgGONeFQf0KpJVcCCPmiLFYCv6vVFwd1O5Pzgk83NQk9EktZ9bAByNu2VMh8 X2aV+oJvinIpuE50q3O6QAC3R4Q8h/Bv2gvO4lOWYkhkJyyO6RIiXs5sGhglAM627V0n 18YhXNDs03kUX60/7hjaN+AH1C99klQgf7ZQ1sx+5a6MSxaayMnyBk3rbJnOhrdiOZKN 8vNFRMno8yE1wx+AvvFAYDoGrT3KE85ab+o5ODrJABnYqnrwVxfn4UZ0oEYn2sLKLcOQ gUCg== X-Received: by 10.180.211.101 with SMTP id nb5mr17943062wic.53.1405255811930; Sun, 13 Jul 2014 05:50:11 -0700 (PDT) Received: from MacBook-Pro-de-jocelyn-fournier.local (ven06-1-82-234-156-61.fbx.proxad.net. [82.234.156.61]) by mx.google.com with ESMTPSA id lk5sm18533597wic.18.2014.07.13.05.50.10 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 13 Jul 2014 05:50:11 -0700 (PDT) Message-ID: <53C28081.10005@gmail.com> Date: Sun, 13 Jul 2014 14:50:09 +0200 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: ajf@ajf.me CC: internals@lists.php.net References: <08503591-EFC8-48E6-984E-FFC292C5EA5F@ajf.me> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] [RFC] Scalar Type Hinting With Casts (re-opening) From: jocelyn.fournier@gmail.com (Jocelyn Fournier) Hi, 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 ?) 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 ! Thanks, Jocelyn Le 13/07/2014 14:09, Nikita Popov a écrit : > On Sun, Jul 13, 2014 at 3:57 AM, Andrea Faulds wrote: > >> Good evening, >> >> PHP’s type hinting system is rather incomplete as it does not support >> scalars, only arrays, callables and objects. In 2012, Anthony Ferrara >> created an RFC which proposed type hinting for scalar types (int, float, >> bool, string and resource) with casting, with behaviour nearly the same as >> that of zend_parse_parameters. Sadly, he later left PHP internals and >> withdrew his RFCs. >> >> Since I am very much in favour of scalar type hints, I’ve updated the >> patch to master and made some minor improvements, and I am re-opening the >> RFC with the intent to try and get it into PHP 5.7. The patch is mostly >> there. It needs some more tests and there are some edge cases I need to >> deal with, but it otherwise works, and I expect I can finish fixing it up >> very soon. >> >> The RFC is here: https://wiki.php.net/rfc/scalar_type_hinting_with_cast >> >> A pull request is here: https://github.com/php/php-src/pull/717 >> >> I’m hoping I can get this into PHP 5.7. I think scalar type hinting would >> be a valuable addition to PHP’s existing type hinting system. >> >> Thanks! >> > > I think the approach described in this proposal is the best way to > implement scalar typehinting, as it allows typical loosely-typed usage > patterns, while still providing type guarantees within the function. > > I haven't yet closely reviewed the details of what is and isn't allowed, > but one things I would strongly recommend against is introducing any > "accept but throw a notice" cases. Either a value is allowed or it isn't. > "12foo" to an int/float argument should throw a recoverable fatal error, > like everything else. (I'd change that in zpp as well, but that's a > different discussion). > > The question of passing floats like 12.5 to an int param is somewhat > tricky. One could argue that it's possible to end up with floats like > 12.0000000001, which are nearly integers but not quite there, and a user > might expect them to behave the same as the integer 12. On the other hand > one can also argue that it's easy to end up with the inverse, namely > 11.999999999999. In this case the end user might also expect it to be > treated as integer 12, however an integer cast will truncate it to 11. > > The truncation behavior of the float to int cast is why I tend towards the > behavior the RFC currently proposes (forbid non-integral floats): It is > rather unlikely that truncation is what was intended, as such an explicit > rounding call is necessary anyways. > > Nikita >