Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77138 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 42816 invoked from network); 10 Sep 2014 17:42:01 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Sep 2014 17:42:01 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.170 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.170 mail-we0-f170.google.com Received: from [74.125.82.170] ([74.125.82.170:35863] helo=mail-we0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A5/E0-28059-86D80145 for ; Wed, 10 Sep 2014 13:42:01 -0400 Received: by mail-we0-f170.google.com with SMTP id u57so5722139wes.1 for ; Wed, 10 Sep 2014 10:41:58 -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=njjAHo8deREZHvv40gPcejVMKNU49rN3NgTeTGSyGus=; b=eq+2Qn4lGpFAf8vCwFbIhW1L3xtmEMsSylixwPYdwC3OPZJVwul+IrfIE2WvVr5TuH zaMP2ynEqfAdPodsSBwl2cv+nsPlFPXqzazdtJPxzcmAaSmaIez83EvXxbPU0suKC+OW mRFsdIusF+iwANu07HE85tkNQ+Z7h/rG8ofAAYD7v5uVuqEHAKOhWfprJzsESY7dsrS+ jvtXYEYdAeth9ucneHdBPxSV35bBS6k4by3eGSDIdgHwRo152UZQaZxCBgNrS4OOZc0f vepHUsUmlQ3yTWimwqv07E0ZW4LT/Z7yqbYmBVo9kfJTlveXXSZMiXkoFQwJys1uBnr/ GnNw== X-Received: by 10.180.210.239 with SMTP id mx15mr568831wic.29.1410370918005; Wed, 10 Sep 2014 10:41:58 -0700 (PDT) Received: from [192.168.0.177] ([62.189.198.114]) by mx.google.com with ESMTPSA id uk7sm19337144wjc.34.2014.09.10.10.41.56 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 10 Sep 2014 10:41:57 -0700 (PDT) Message-ID: <54108D63.3070707@gmail.com> Date: Wed, 10 Sep 2014 18:41:55 +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: <004d01cfcc63$e7e3ac40$b7ab04c0$@tutteli.ch> <3DB7DEC9-2F99-4DDB-95A9-70455E4BABCB@ajf.me> In-Reply-To: <3DB7DEC9-2F99-4DDB-95A9-70455E4BABCB@ajf.me> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] make casts more strict in PHP 7 From: rowan.collins@gmail.com (Rowan Collins) Andrea Faulds wrote (on 09/09/2014): > If people want strict casting, we can add new functions or operators for that specifically. 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). 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 = safe_cast($_GET['i'], 'int'); 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 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(): PHP_TYPE_INT = "integer" PHP_TYPE_FLOAT = "double" PHP_TYPE_STRING = "string" PHP_TYPE_ARRAY = "array" PHP_TYPE_OBJECT = "object" PHP_TYPE_RESOURCE = "resource" PHP_TYPE_NULL = "NULL" Of course, we'd then need to agree exactly which casts are considered "safe"... -- Rowan Collins [IMSoP]