Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98753 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 11606 invoked from network); 10 Apr 2017 02:05:05 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Apr 2017 02:05:05 -0000 Authentication-Results: pb1.pair.com smtp.mail=yohgaki@ohgaki.net; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yohgaki@ohgaki.net; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ohgaki.net designates 180.42.98.130 as permitted sender) X-PHP-List-Original-Sender: yohgaki@ohgaki.net X-Host-Fingerprint: 180.42.98.130 ns1.es-i.jp Received: from [180.42.98.130] ([180.42.98.130:53942] helo=es-i.jp) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 14/46-08240-C48EAE85 for ; Sun, 09 Apr 2017 22:05:02 -0400 Received: (qmail 73470 invoked by uid 89); 10 Apr 2017 02:04:57 -0000 Received: from unknown (HELO mail-qt0-f176.google.com) (yohgaki@ohgaki.net@209.85.216.176) by 0 with ESMTPA; 10 Apr 2017 02:04:57 -0000 Received: by mail-qt0-f176.google.com with SMTP id c45so52597573qtb.1 for ; Sun, 09 Apr 2017 19:04:56 -0700 (PDT) X-Gm-Message-State: AN3rC/6jChjOe3RWjEDF17lzN50lKEJ5wiXJRsmDt8NeQjCHmXFy3HA+KeD/TsrC9xsgUHwFfH+WbBZqB0nM4A== X-Received: by 10.200.57.18 with SMTP id s18mr6905167qtb.1.1491789890060; Sun, 09 Apr 2017 19:04:50 -0700 (PDT) MIME-Version: 1.0 Received: by 10.140.101.109 with HTTP; Sun, 9 Apr 2017 19:04:09 -0700 (PDT) In-Reply-To: References: Date: Mon, 10 Apr 2017 11:04:09 +0900 X-Gmail-Original-Message-ID: Message-ID: To: Rasmus Schultz Cc: PHP internals Content-Type: multipart/alternative; boundary=001a113e535e7a16a2054cc6667c Subject: Re: [PHP-DEV] scalar type-casting From: yohgaki@ohgaki.net (Yasuo Ohgaki) --001a113e535e7a16a2054cc6667c Content-Type: text/plain; charset=UTF-8 Hi Rasmus, Although DbC is not what you need, but DbC could solve your issue more efficiently. i.e. Faster execution, not shorter code. https://wiki.php.net/rfc/dbc2 With DbC, caller has responsibility to pass correct parameters. On Sun, Apr 9, 2017 at 6:30 PM, Rasmus Schultz wrote: > > $one = "1"; > $one_int = (int) $one; > add_one($one_int); > add_one(&$value) require (is_int($value)) { $value += 1; } // Caller has responsibility to pass correct parameters. $one = filter_validate($_GET['var'], FILTER_VALIDATE_INT); add_one($one); > class Foo { public function __toString() { return "foo"; } } > function append_to(string &$str) { $str .= "_bar"; } > $foo = new Foo(); > append_to($foo); > var_dump($foo); // string(7) "foo_bar" class Foo { public function __toString() { return "foo"; } } function append_to(&$str) require (is_string($str)) { $str .= "_bar"; } $foo = new Foo(); // Caller has responsibility to pass correct parameters, but it's not append_to($foo); // Error at DbC precondition check in append_foo() var_dump($foo); // Cannot reach here in dev mode I really like parameter type check. Problem is type check makes execution slower. Another problem is type check is not enough for many codes. With DbC support, we can specify any expressions. Therefore, we can check much more complex requirements for functions/methods at development time. e.g. function save_age($user_age) require (is_int($user_age)) require ($user_age >= 0) require ($user_age < 150) { save_to_somewehre($user_age); } //Note: All input parameters must be validated to be correct value for the app. e.g. use filter_validate()/etc What you really need might be DbC. Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net --001a113e535e7a16a2054cc6667c--