Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75396 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 31196 invoked from network); 13 Jul 2014 13:20:46 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jul 2014 13:20:46 -0000 Authentication-Results: pb1.pair.com smtp.mail=addw@phcomp.co.uk; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=addw@phcomp.co.uk; sender-id=permerror Received-SPF: pass (pb1.pair.com: domain phcomp.co.uk designates 78.32.209.33 as permitted sender) X-PHP-List-Original-Sender: addw@phcomp.co.uk X-Host-Fingerprint: 78.32.209.33 freshmint.phcomp.co.uk Received: from [78.32.209.33] ([78.32.209.33:41548] helo=mint.phcomp.co.uk) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DF/82-16748-DA782C35 for ; Sun, 13 Jul 2014 09:20:46 -0400 Received: from addw by mint.phcomp.co.uk with local (Exim 4.72) (envelope-from ) id 1X6Jhq-0006Zj-LP for internals@lists.php.net; Sun, 13 Jul 2014 14:20:42 +0100 Date: Sun, 13 Jul 2014 14:20:42 +0100 To: internals@lists.php.net Message-ID: <20140713132042.GU6540@phcomp.co.uk> Mail-Followup-To: internals@lists.php.net References: <08503591-EFC8-48E6-984E-FFC292C5EA5F@ajf.me> <53C2178E.7070705@sugarcrm.com> <53C24061.6050503@lsces.co.uk> <20140713101840.GR6540@phcomp.co.uk> <2f218b4d8cffb1a55e464e715c319b1f@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2f218b4d8cffb1a55e464e715c319b1f@mail.gmail.com> Organization: Parliament Hill Computers Ltd User-Agent: Mutt/1.5.20 (2009-12-10) Subject: Re: [PHP-DEV] [RFC] Scalar Type Hinting With Casts (re-opening) From: addw@phcomp.co.uk (Alain Williams) On Sun, Jul 13, 2014 at 02:35:43PM +0300, Zeev Suraski wrote: > Type juggling is an *inherent* feature of PHP. Paraphrasing what Rasmus > said several years ago(*), 'if you implement this [strong typing], please > don't call it PHP'. > > Again, luckily, what Andrea proposes isn't strong typing. It still > maintains the most important properties of type juggling - e.g. the > ability to work with strings coming in as input (GET/POST/etc.) without > having to worry about typing. I think that you need to look at how and where this will be used. In (what I shall call) the outer parts of programs it may not be useful, but it will in the inner parts. Let me explain: The outer part of a program which will work with things like $_GET['age'] which might have values like: '10', 'ten', '10.5', '' or even be unset. Even assigning a value to '$age' and have PHP do type checking is not what you want. Type juggling is an important part of outer code. Input (form) variables are then type/... checked by validation tests, this will reject 'ten', '', unset and maybe '10.5'. I see this as a kind of firewall. The inner side only uses values that have been through the 'firewall', ie I know that $age will contain an integer. I can then do integer operations on it without further checking - indeed I do NOT want to have to do further checking since that just adds to the code size/complexity/ugliness. It is in this 'inner' code where I will want to use PHP type hinting as currently discussed. Although my values should have been though the firewall, I do make mistakes, etc, so the inner code either occasionally fails in an obscure way or assertions are done. Some means of having PHP help here would be very much appreciated. Which of the two below is clearer: function CanDrive($age) { if( !is_numeric($age)) throw new Error("\$age is not integer (CanDrive())"); return $age >= 17; } function CanDrive(int $age) { return $age >= 17; } The above is how I write programs. [I do realise that the 2 above are not exactly the same.] > (*) I couldn't find that email quickly enough so apologies if I'm not > completely accurate on this. -- Alain Williams Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer. +44 (0) 787 668 0256 http://www.phcomp.co.uk/ Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php #include