Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:86153 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 9858 invoked from network); 11 May 2015 07:21:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 May 2015 07:21:25 -0000 Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.173 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.214.173 mail-ob0-f173.google.com Received: from [209.85.214.173] ([209.85.214.173:35571] helo=mail-ob0-f173.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 31/30-03799-37850555 for ; Mon, 11 May 2015 03:21:23 -0400 Received: by obcus9 with SMTP id us9so63958367obc.2 for ; Mon, 11 May 2015 00:21:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=jlKf4bhFCknodNznF5Wz8eYsVcjymOUUAV+Ix8yQyEg=; b=ch4xIV1k/mKz3vCBLKIjKVt6rYKtiuhX0uxkGm8DF5Bkbew8vR+ljsWrwzGEPLx/FG pEU0EPtvYbNOkhBNwv3gjCan7MmCehYLmQH7C3WzJ7y/13+vRR3EdEuudZiZ273Mvh7O l1I5RHKSFEvghlmEQ0N7JBaqWYFXk4BpJM2c/uuAd4sUWf0NT7clE5kYpQd0u63qsCv8 IbBgYM3Si51oDRHnHNxtD/fZr48JSuQBpNXhYJmF2aahBbplLFF95rwcbG4/2CIKEG3s hsGMg6/Wup4W/0mOFXvPX0t2gUf4fsnxy/liu4J6GdDL3qomfgMSZ9M40NXy/Z6wn0GE 7LQA== X-Received: by 10.202.194.214 with SMTP id s205mr6664822oif.39.1431328879962; Mon, 11 May 2015 00:21:19 -0700 (PDT) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.202.104.196 with HTTP; Mon, 11 May 2015 00:20:39 -0700 (PDT) In-Reply-To: References: Date: Mon, 11 May 2015 16:20:39 +0900 X-Google-Sender-Auth: JlU7Fy5Bvj78za_bgScSVIEKR4Y Message-ID: To: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a113d219472752b0515c939e3 Subject: Re: Adding "numeric" type hint From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a113d219472752b0515c939e3 Content-Type: text/plain; charset=UTF-8 Hi all, On Wed, Apr 29, 2015 at 8:50 AM, Yasuo Ohgaki wrote: > External data can have any form of numbers. > Current PHP can handle them as "string". However PHP7's type hint cannot > handle numeric data well because it only has "int" and "float" hints. > > http://3v4l.org/6J0bZ > > There are cases that programmer want/need to handle any numeric values. > e.g. JSON numeric, Database numeric, PHP's array key beyonds INT min/max, > Values for BCMath/GMP, etc. It's common for 32 bit platforms. For example, > we cannot do query database safely with "int" type hint. > > e.g. > function get_some_db_record(int $id) {} > > Most databases uses 64 bit signed int for database IDs, but this code > limits > $id to 32 bit signed int for 32 bit platforms. There are databases that > allow > unsigned 64 bit int ID. > > To avoid this problem, users must use "string" type hint and have to > validate > parameter by themselves. This ruins benefits of type hint. Most PHP will > not > use "string" type hint even if apps need large numbers. > > How about have "numeric" type hint that accepts any format/class(GMP) > of numeric values? > > function foo(numeric $var) { // do something useful with numeric value } > > To be honest, I would like to have StrictSTH RFC behavior for weak mode > int/float type hints to make sure "int"/"float" has integer/float > form/value always, > but "numeric" type hint may do the job. One function with "int"/"float" > type hint > could break app with current type hint implementation, though. i.e. > Working > app suddenly emits fatal error and exits when database/json returns value > beyond int/float. > > http://3v4l.org/6J0bZ (See how it works with large string integer value) > > Any comments? > I've never wrote my blog in English, but I wrote one because peice by piece discussion is not going to anywhere. http://blog.ohgaki.net/dont-use-php7-type-hint-for-external-data How many of you think current scalar type hint is useful enough to interact with database/json/xml/yaml/rest/etc? We need "numeric" hint at least. IMHO. If not, we need "large warning sign" in documentation as a last resort at least. Regards, P.S. We may be better to declare 32 bit CPU support EOL by PHP 7 to reduce the impact. We'll have the same issue when 128 bit CPU or 128 bits IEEE 754 float became in common, though. -- Yasuo Ohgaki yohgaki@ohgaki.net --001a113d219472752b0515c939e3--