Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75422 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 73133 invoked from network); 13 Jul 2014 15:51:28 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jul 2014 15:51:28 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.173 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.173 mail-we0-f173.google.com Received: from [74.125.82.173] ([74.125.82.173:58063] helo=mail-we0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CB/6B-16748-FFAA2C35 for ; Sun, 13 Jul 2014 11:51:27 -0400 Received: by mail-we0-f173.google.com with SMTP id t60so3080389wes.32 for ; Sun, 13 Jul 2014 08:51:23 -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=BUvtQqz0vt8tw8A9TMaBJGzodz5wn8md85SUF0M7vxU=; b=LdaP7QTmhpvTO+mydVJq0Q9xFVDj1V0FQlEyZ+zVGVaofPcydYZHWQvv5wHYcKjUCT 6GDYVnnq76kjjh2UHFQ55fXnD0G626cbplG9MduvUnpEva0bqDCuW67cRlYKDrPLmMyB QwjKQslceKQiyKx1FmC3x4kas8rqwEBOyk0NVgN02fhLXxErbgYf+R98oQhkBLpw0plX nvvsmuLQSXDBSF7ubHmVKMTBPK9tpWaW1Mj7FNnQENOwkepRrC2KrJYE9uJPpwoK6OQ+ pshVQs4d6NfeP5vyrrubeKD/uopPzm6Tz7UbJCMi4hZunC3cWT1pb5BpNa3ToaajBb7j E46g== X-Received: by 10.194.60.110 with SMTP id g14mr3103754wjr.101.1405266683492; Sun, 13 Jul 2014 08:51:23 -0700 (PDT) Received: from [192.168.0.2] (cpc19-brig17-2-0-cust25.3-3.cable.virginm.net. [81.101.201.26]) by mx.google.com with ESMTPSA id ec8sm20160197wic.10.2014.07.13.08.51.22 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 13 Jul 2014 08:51:22 -0700 (PDT) Message-ID: <53C2AAF7.3060504@gmail.com> Date: Sun, 13 Jul 2014 16:51:19 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: 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> <53C2A753.2060001@gmail.com> In-Reply-To: <53C2A753.2060001@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Scalar Type Hinting With Casts (re-opening) From: rowan.collins@gmail.com (Rowan Collins) On 13/07/2014 16:35, Jocelyn Fournier wrote: >> 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'; > > ? :) Well, the ability to cast to an object at all is pretty wacky, since PHP has no standard base class for objects, nor any mechanism to cast to or from specific classes, etc. Perhaps this example is more appropriate: function wants_array(array $foo) { var_dump($foo); } wants_array('abc'); // Error wants_array( (array)'abc' ); // OK - parameter passed is array(0 => 'abc') As you can see, it's perfectly possible for the value to be cast, but the "type hint" does not do so, it rejects the value. Previous proposals (e.g. [1]) have suggested a distinct syntax for the "automatic cast" which you seem to be favouring, which would allow us to write this, avoiding the inconsistency: function casts_to_array((array) $foo) { var_dump($foo); } casts_to_array('abc'); // OK - parameter interpreted as (array)'abc' and thus array(0 => 'abc') This is just sugar for: function casts_to_array($foo) { $foo = (array)$foo; var_dump($foo); } As such, it neither requires, nor is required for, additional warnings around lossy casts. [1]: http://blog.ircmaxell.com/2012/03/parameter-type-casting-in-php.html -- Rowan Collins [IMSoP]