Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75418 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 67091 invoked from network); 13 Jul 2014 15:35:54 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jul 2014 15:35:54 -0000 Authentication-Results: pb1.pair.com smtp.mail=jocelyn.fournier@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=jocelyn.fournier@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.43 as permitted sender) X-PHP-List-Original-Sender: jocelyn.fournier@gmail.com X-Host-Fingerprint: 74.125.82.43 mail-wg0-f43.google.com Received: from [74.125.82.43] ([74.125.82.43:45832] helo=mail-wg0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 06/1A-16748-857A2C35 for ; Sun, 13 Jul 2014 11:35:53 -0400 Received: by mail-wg0-f43.google.com with SMTP id l18so2298309wgh.26 for ; Sun, 13 Jul 2014 08:35:50 -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:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=h/4i/B5bzRFJ84xTWAb0OMleSfa5dm91E73mipoHjAk=; b=qM7mAjKE3UkZLXcNSO7GuROIb1/6RSX8E7sg9saCckwVe2Y3GxbAmzxNr54+yEupgz lumdNJ7OkuohMn1vsRlpc6rsrELw3U7634Nll3qOVMAznfnP4m9uXiLIaUGGNV6sAaBt BV/XuLz4SQpRv/Uc7+APShZnHnC06GQDweTlIE15uUO9vwd1chH4UYDsGBsIINvPwr5a DO0lNoQC1dxsy2ikXafpwPrSwi/wqec/lSg8Gmg8+Mk+Eb4W8Bj2X45KYm3Mncp7X+my O9a1/Wt5B20pjngAcEvD30U9C/Sn6nz0GXNiTVnmjGEH0SlIxgMeXEf/E6rRr6L4w4EC sD/Q== X-Received: by 10.180.206.15 with SMTP id lk15mr18153766wic.43.1405265750081; Sun, 13 Jul 2014 08:35:50 -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 nf11sm20035862wic.9.2014.07.13.08.35.48 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 13 Jul 2014 08:35:49 -0700 (PDT) Message-ID: <53C2A753.2060001@gmail.com> Date: Sun, 13 Jul 2014 17:35:47 +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: Rowan Collins , internals@lists.php.net References: <08503591-EFC8-48E6-984E-FFC292C5EA5F@ajf.me> <027E65EF-C4FC-474C-92BB-D99EFADDEEED@ajf.me> <53C29EE4.3090808@gmail.com> <53C2A2F7.1000806@gmail.com> In-Reply-To: <53C2A2F7.1000806@gmail.com> 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) Le 13/07/2014 17:17, Rowan Collins a écrit : > On 13/07/2014 15:59, Jocelyn Fournier wrote: >> From my point of view, if the type annotations are doing implicit cast >> (with or without E_NOTICE/E_STRICT warning), they should behave >> exactly the same than an explicit cast. If it behaves differently, >> I'll be really difficult for a developer to guess what will be the PHP >> behaviour with this new syntax. > > The problem is, in PHP an explicit type cast never fails - (int)'abc' > simply gives you the value 0, not an error. Yes, but if you add a E_NOTICE / E_STRICT warning with this cast, you'll be aware something wrong could happen with this code, and you'd better explicit cast your variable. > If you let scalar typehints > just generate unchecked casts, we'd have this: > > wants_object(object $foo) { var_dump($foo); } > wants_int(int $foo) { var_dump($foo); } > > wants_object('abc'); // fails with E_RECOVERABLE_ERROR > wants_int('abc'); // succeeds and prints int(0) Actually it would be wants_object('abc'); // fails with E_RECOVERABLE_ERROR wants_int('abc'); // succeeds and prints int(0) + E_NOTICE / E_STRICT warning > > That seems both inconsistent and less useful than a hybrid juggling + > validation approach. Than means you find currently inconsistant than $foo = (int) 'abc'; works but not $bar = (object) 'abc'; ? :) Jocelyn