Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:77139 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46959 invoked from network); 10 Sep 2014 18:27:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Sep 2014 18:27:45 -0000 Authentication-Results: pb1.pair.com header.from=mike.php.net@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=mike.php.net@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.172 as permitted sender) X-PHP-List-Original-Sender: mike.php.net@gmail.com X-Host-Fingerprint: 209.85.212.172 mail-wi0-f172.google.com Received: from [209.85.212.172] ([209.85.212.172:54352] helo=mail-wi0-f172.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 58/00-45675-02890145 for ; Wed, 10 Sep 2014 14:27:45 -0400 Received: by mail-wi0-f172.google.com with SMTP id q5so3547186wiv.5 for ; Wed, 10 Sep 2014 11:27:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=HgH6IrPpkNmf/leYJpeo/RXW8vzYr62ivIRobMhFbaQ=; b=gKidUW6pUIbQvge1ffySN608XcVO4ZzADFNUCaIPav/CUuujcKUNPyEU5BAgn5I0Aw zQByES2AnqFOzc9BH0MWGIuxonxJVjzHlpUQ2KjiBriCEVgxpjLLymo26oz0kF9OZHwx f2WFaiDBnVQ58aJLZGXhfG+KTeBybvmZoqTY7tdKXs5s6GoEV7j8x6OYg62QijQE7Q4U 7IRxIJeX3RWnyZyMUEJUfljVM3JrCSy4gE7ic+O3vRp/U8TXA/r7KICjdOmiFI2AQ3jo g0MY0qqVWyTX2vexSM4vedduvtxA/u09tAWLBqtWHSSG2zOVJ/eq9RjK5UVK9guIPSu1 tL7w== X-Received: by 10.194.184.166 with SMTP id ev6mr50583972wjc.61.1410373661157; Wed, 10 Sep 2014 11:27:41 -0700 (PDT) Received: from [192.168.4.120] (178-18-170-101.customer.bnet.at. [178.18.170.101]) by mx.google.com with ESMTPSA id am1sm19470230wjc.29.2014.09.10.11.27.40 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 10 Sep 2014 11:27:40 -0700 (PDT) Sender: Michael Wallner Message-ID: <5410981B.5000207@php.net> Date: Wed, 10 Sep 2014 20:27:39 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.0 MIME-Version: 1.0 To: Rowan Collins , internals@lists.php.net References: <004d01cfcc63$e7e3ac40$b7ab04c0$@tutteli.ch> <3DB7DEC9-2F99-4DDB-95A9-70455E4BABCB@ajf.me> <54108D63.3070707@gmail.com> In-Reply-To: <54108D63.3070707@gmail.com> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] make casts more strict in PHP 7 From: mike@php.net (Michael Wallner) On 10/09/14 19:41, Rowan Collins wrote: > 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"... > .oO(ext/filter) ? -- Regards, Mike