Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:80597 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 88778 invoked from network); 15 Jan 2015 22:00:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jan 2015 22:00:42 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.45 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.45 mail-wg0-f45.google.com Received: from [74.125.82.45] ([74.125.82.45:36116] helo=mail-wg0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DE/35-61348-88838B45 for ; Thu, 15 Jan 2015 17:00:42 -0500 Received: by mail-wg0-f45.google.com with SMTP id y19so17505714wgg.4 for ; Thu, 15 Jan 2015 14:00:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=user-agent:in-reply-to:references:mime-version :content-transfer-encoding:content-type:subject:from:date:to :message-id; bh=gn7+OesKKoqfgFJTTTNNMa/yLtgYUdXfFDsnYrVYMrQ=; b=PUbVxjExRdDzZCXv/GuOaSDIN8plXvr1OkjydJkJgylAPVBVTQQva+WX6ok3AFLaC6 TZ2ESb8g4mpjNSrOpSOvdnhraEZpfBCM+ZkSkjHwbIvyicOhPcYcLzXPhXsezfLudJcr oGWyHZWyYecFT232pTnJlX1GU+Lfw59nc1lWhmyGTTsbIRWCwi4dxvShZTMdR3PIUUhe m4qxmTO0G2FU+dRty3eTTrhUOGecrjqPeoU/JpuyQZJjVztiyzNvijRVmT3BbFK7z7GW ip+vhWkkDt6dpX1C0z8wTusA4Y6UBGHk5BHdNmYJdrck5Wy1bWKRjNavaA5jc8f5HKLL RD2A== X-Received: by 10.180.198.240 with SMTP id jf16mr63353wic.27.1421359233662; Thu, 15 Jan 2015 14:00:33 -0800 (PST) Received: from [192.168.0.3] (cpc68956-brig15-2-0-cust215.3-3.cable.virginm.net. [82.6.24.216]) by mx.google.com with ESMTPSA id b10sm3652956wjr.32.2015.01.15.14.00.32 for (version=TLSv1.2 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 15 Jan 2015 14:00:32 -0800 (PST) User-Agent: K-9 Mail for Android In-Reply-To: References: <8DCD1B72-C81D-499E-B455-E4A042CD76E6@ajf.me> <4E2073DE-0951-498C-97BB-DDAC094F11FA@ajf.me> <9a033dd1f223f854e760924d118ab812@mail.gmail.com> <2ae0164cb9b9bf1c974d7a3c60af0466@mail.gmail.com> <6105ea99002e634373c09685310e26a6@mail.gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Date: Thu, 15 Jan 2015 21:58:28 +0000 To: PHP Internals List Message-ID: <3B70D231-BA42-497B-9D71-D8369C102F8D@gmail.com> Subject: Re: [PHP-DEV] [RFC] Scalar Type Hints v0.2 From: rowan.collins@gmail.com (Rowan Collins) On 15 January 2015 19:20:27 GMT, Andrey Andreev wrote: >Hi, > >On Thu, Jan 15, 2015 at 7:13 PM, Andrea Faulds wrote: >> >>> Now from that perspective I cannot rely that I am in strict and >would have to handle the default weak even although I declared in my >class that i wanted strict mode which only affected the code inside of >that file. That's ultimately what I am saying and why I am stating it >would have to handle both conditions. In reality, you'd always act as >if you were in a weak mode supporting that case. >> >> I… really don’t understand, I’m sorry. Are you complaining that if a >caller uses the weak mode, they are not bound by strict types? Why do >you want to affect the code of the caller? > >Consider this badly designed API: > > declare(strict_typehints=TRUE); > > const MODE_APPEND = 0; > const MODE_TRUNCATE = 1; > const MODE_PREPEND = 2; > > function write_file(string $path, int $mode, string $data) > { > // it's quite obvious > } > >Somewhere else, you forget the parameters order ... > > declare(strict_typehints=FALSE); > > $path = '/important/file.dat'; > $data = '1 year'; > $mode = MODE_APPEND; > > write_file($path, $data, $mode); > >It's a bad example - an awful (and indeed, very much useless) API, >combined with an absent-minded consumer. Yet, it demonstrates how >putting the caller in control *can* cause a disaster. But, crucially, it causes a disaster *for the caller*. The library function gets exactly the types it expects, and can happily pass them to some other typehinted function, including an internal/extension function. The "disaster" is that the effect is not what the caller intended, which would be true for all sorts of other mistakes. No amount of strict type hinting will solve my inability to remember whether strpos() takes haystack, needle or needle, haystack. Presented with a function typehinted as foo(int $a), the same people who'd write code in "weak mode" under the current proposal would probably just change foo($_GET['id']) to foo((int) $_GET['id']), with exactly the same effect. There is absolutely nothing the author of foo() can do to stop them, no matter what options we give at the definition site. The sheer difficulty people are having understanding the "caller is in control" concept probably counts as its biggest flaw, though... -- Rowan Collins [IMSoP]