Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80049 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 34088 invoked from network); 1 Jan 2015 15:29:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Jan 2015 15:29:16 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.199 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.199 imap11-2.ox.privateemail.com Received: from [192.64.116.199] ([192.64.116.199:48735] helo=imap11-2.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 14/4B-60454-CC765A45 for ; Thu, 01 Jan 2015 10:29:16 -0500 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 3FE138800D5; Thu, 1 Jan 2015 10:29:13 -0500 (EST) X-Virus-Scanned: Debian amavisd-new at imap11.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap11.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 04nS30977puY; Thu, 1 Jan 2015 10:29:13 -0500 (EST) Received: from [192.168.0.13] (unknown [94.13.96.117]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id 2AF668800DB; Thu, 1 Jan 2015 10:29:12 -0500 (EST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.1 \(1993\)) In-Reply-To: Date: Thu, 1 Jan 2015 15:28:40 +0000 Cc: Zeev Suraski , PHP Internals Content-Transfer-Encoding: quoted-printable Message-ID: <01B31687-8613-45BD-8DEF-6E5D100627D7@ajf.me> References: <41D5BB0B-73AF-488E-968D-90B2878E3178@ajf.me> <16d442a345cf2cbd4faac6d068c57343@mail.gmail.com> To: Maxime Veber X-Mailer: Apple Mail (2.1993) Subject: Re: [PHP-DEV] [RFC] Scalar Type Hints From: ajf@ajf.me (Andrea Faulds) Hi Maxime, > On 1 Jan 2015, at 13:29, Maxime Veber wrote: >=20 > I was expected an RFC like this in PHP for a while. I'm happy somebody = made one, thanks. Glad to hear that. > But something hit me in that. even if you can't give an objet, you can = give any scalar type that will be cast. > I'm not sure this behavior is very relevant. >=20 > Actually if I ask for a string, why the user could be specify an int = without getting error ? >=20 > Consider the following PHP 5.6 code: >=20 > function foo($a) { > if (!is_string($a)) { > throw new \Exception('You need to give a string'); > } > } >=20 > Using your type hinting does not fix the problem of string checking = because the value will just be cast if it's an integer. Ofc if the user = give an object it's ok. > But why not throw a message error in any cases ? For various reasons, PHP has always been a weakly-typed language, so we = allow conversions between scalar types when they make sense. An integer = isn=E2=80=99t a string, sure, but it can be simply converted to one and = the result makes sense, so I think that=E2=80=99s why we allow it. It = would still error in some cases, like an object without __toString for a = string parameter, or an array for an integer parameter. Thanks. -- Andrea Faulds http://ajf.me/