Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81740 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 65861 invoked from network); 3 Feb 2015 20:52:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Feb 2015 20:52:33 -0000 Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.192.47 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.192.47 mail-qg0-f47.google.com Received: from [209.85.192.47] ([209.85.192.47:33158] helo=mail-qg0-f47.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 25/79-20608-11531D45 for ; Tue, 03 Feb 2015 15:52:33 -0500 Received: by mail-qg0-f47.google.com with SMTP id l89so2715684qgf.6 for ; Tue, 03 Feb 2015 12:52:30 -0800 (PST) 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:cc:content-type; bh=JsjyyD4jHNr2skaJFwgZ5JEmFVU4bCUeutBTRGht6CE=; b=fWg2DuN/Etarlv7lVUZcNBhlsWERMmp01Iz6EMmG4RoFeaojXIZBMCM7oUrBUJ1WeK xfMvPCucDlbvQIjp9FvsrlXd/f6bXD1Gqv5DNqeOvJYYRBKj3x6y4a8A+OjwmeXKkjBj 5YZBnmYqra2vQ5cbmBGWO+7Jkyf4+ZBx6F5lTojicX0oiBU9HMFGN4yHgktukDAUn4i4 cyRMUddznJcE5ZskZgKN/hwobL4NO//FwBYdmSQwHzV2SpHzLhQuKwbc3xD90pPXjSA5 URbBY+LyGgci1w7OhkMgZKYzyI5/maZKb4RW4NszKSXVzXwbGdpBzWbhKYcqYcODrdTZ oF+g== X-Received: by 10.140.90.112 with SMTP id w103mr49800474qgd.65.1422996750803; Tue, 03 Feb 2015 12:52:30 -0800 (PST) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.229.28.72 with HTTP; Tue, 3 Feb 2015 12:51:50 -0800 (PST) In-Reply-To: <20150203202336.D5D5626193D@dd15934.kasserver.com> References: <20150202210349.6FB91261948@dd15934.kasserver.com> <54CFE965.2080905@gmx.de> <20150202214254.BEB88261948@dd15934.kasserver.com> <54CFFC5D.4000802@gmx.de> <20150203200757.6b29a758@main> <20150203202336.D5D5626193D@dd15934.kasserver.com> Date: Wed, 4 Feb 2015 05:51:50 +0900 X-Google-Sender-Auth: 3e8f-Bi59Buq7sM-Qa3CGW85S_w Message-ID: To: Thomas Bley Cc: Dmitry Stogov , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a11c11a98d929b6050e353fad Subject: Re: [PHP-DEV] What do we need strict scalar type hints for? From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a11c11a98d929b6050e353fad Content-Type: text/plain; charset=UTF-8 Hi all, On Wed, Feb 4, 2015 at 5:23 AM, Thomas Bley wrote: > function addVat($amount) { > validateAmount($amount); > return round($amount*1.19, 2); > } > > function validateAmount($amount) { > if (!is_int($amount) && !is_float($amount)) { > throw new InvalidArgumentException('Argument amount must be of the > type int|float, '.gettype($amount).' given'); > } > } > I use such code where runtime check is required. I prefer following for better performance/readability. i.e. Pre/Post conditions are inside of function definition. If PHP supports native DbC, runtime checks are omitted and script runs faster. NOTE: Dev time checks are done in in/out block with proper DbC design. Runtime check should be in function body. function addVat($amount) in { // precondition check assert(!is_int($amount) && !is_float($amount)); // OR if (!is_int($amount) && !is_float($amount)) { throw new InvalidArgumentException('Argument amount must be of the type int|float, '.gettype($amount).' given'); } } out { // postcondition check assert(is_numeric($__return_vlaue) && $__return_value > 0 && $__return_value < MAX_AMOUNT); } { // function body return round($amount*1.19, 2); } Type hints are useful for simple runtime check. Much less lines to write ;) It also encourage users to validate their app inputs. This is important benefits for secure apps. Things like these encourage proper DbC design naturally. Therefore, I'm +1 for all of these. Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net --001a11c11a98d929b6050e353fad--