Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77145 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 77200 invoked from network); 11 Sep 2014 02:03:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Sep 2014 02:03:43 -0000 Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 192.64.116.200 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 192.64.116.200 imap1-2.ox.privateemail.com Received: from [192.64.116.200] ([192.64.116.200:56559] helo=imap1-2.ox.privateemail.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 92/D0-05626-EF201145 for ; Wed, 10 Sep 2014 22:03:43 -0400 Received: from localhost (localhost [127.0.0.1]) by mail.privateemail.com (Postfix) with ESMTP id 50873B0008A; Wed, 10 Sep 2014 22:03:39 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at imap1.ox.privateemail.com Received: from mail.privateemail.com ([127.0.0.1]) by localhost (imap1.ox.privateemail.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 0LHf7Gv3qNgW; Wed, 10 Sep 2014 22:03:39 -0400 (EDT) Received: from oa-res-27-90.wireless.abdn.ac.uk (oa-res-27-90.wireless.abdn.ac.uk [137.50.27.90]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.privateemail.com (Postfix) with ESMTPSA id 1B61EB00087; Wed, 10 Sep 2014 22:03:37 -0400 (EDT) Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) In-Reply-To: <54108D63.3070707@gmail.com> Date: Thu, 11 Sep 2014 03:03:34 +0100 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: References: <004d01cfcc63$e7e3ac40$b7ab04c0$@tutteli.ch> <3DB7DEC9-2F99-4DDB-95A9-70455E4BABCB@ajf.me> <54108D63.3070707@gmail.com> To: Rowan Collins X-Mailer: Apple Mail (2.1878.6) Subject: Re: [PHP-DEV] make casts more strict in PHP 7 From: ajf@ajf.me (Andrea Faulds) On 10 Sep 2014, at 18:41, Rowan Collins wrote: > I think this came up during the previous discussion, and I actually = think it would be quite nice to have some functions both to perform a = "strict"/"safe" cast (and raise E_CATCHABLE_ERROR or perhaps throw = TypeCastException), and to check if a variable *can* be thus cast = (validating integers is unnecessarily complicated at the moment). >=20 > Either: > mixed safe_cast(mixed $var, string $type) > boolean can_safe_cast(mixed $var, string $type) > e.g. > if ( can_safe_cast($_GET['i'], 'int') ) > $i =3D safe_cast($_GET['i'], 'int'); >=20 > Or, to avoid representing the type with a string arg, a whole set of = functions: > int safe_cast_int(mixed $var) > boolean can_safe_cast_int(mixed $var) > float safe_cast_float(mixed $var) > boolean can_safe_cast_float(mixed $var) > etc >=20 > The first avoids creating so many new functions, but perhaps some = constants should be defined for the argument, based on the values = returned by gettype(): >=20 > PHP_TYPE_INT =3D "integer" > PHP_TYPE_FLOAT =3D "double" > PHP_TYPE_STRING =3D "string" > PHP_TYPE_ARRAY =3D "array" > PHP_TYPE_OBJECT =3D "object" > PHP_TYPE_RESOURCE =3D "resource" > PHP_TYPE_NULL =3D "NULL" >=20 >=20 > Of course, we'd then need to agree exactly which casts are considered = "safe"=85 You know, =93int=94, =93float=94, =93string=94 etc. aren=92t currently = reserved words. We could add functions, or even make them reserved words = and add internal functions (=E0 la isset), so int(=912=92), = float(=912.0=92), string(=91test=92) etc. Or perhaps class-like constructors? $a =3D new int('2'); ? Would kinda = make sense given some people want to add methods to primitive types. -- Andrea Faulds http://ajf.me/