Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:44635 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 13563 invoked from network); 2 Jul 2009 11:33:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Jul 2009 11:33:15 -0000 Authentication-Results: pb1.pair.com header.from=piccoloprincipeazzurro@gmail.com; sender-id=pass; domainkeys=bad Authentication-Results: pb1.pair.com smtp.mail=piccoloprincipeazzurro@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.227 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: piccoloprincipeazzurro@gmail.com X-Host-Fingerprint: 209.85.219.227 mail-ew0-f227.google.com Received: from [209.85.219.227] ([209.85.219.227:40365] helo=mail-ew0-f227.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 87/20-12594-7FA9C4A4 for ; Thu, 02 Jul 2009 07:33:14 -0400 Received: by ewy27 with SMTP id 27so1779942ewy.23 for ; Thu, 02 Jul 2009 04:33:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=IaONRntkn1Wlz235M1llt+yMjyK6fxk0pt3u50cTNX8=; b=tcsLvLxsjWG+hroJRUo/0vccbmOeFc52O9I+nSRCGJ6l1rlWklm6Mg/u9+TsA9GNPf xu7iI1XRltlEs+6IKfI7cfOABBnuiJZTsSBNka4kTI8JuY1SqGvob88MPi7yM+wEknMM hYULn23Wy5je9HGp69gaPRF6xR8pD3amvmPO0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=LgPVq0K7Tl2+gadPrXSqRKYMMT/7vPNklQ8MGLFLDsMl3oHzkOZTuRD2jHvTn2NgGK oPH112Y2e9ib0nbJo3nert2qtl5lCB4C8rw6MVsB5JC2X7FZQCoM+asDcN7utR9MQt5q vcdNueGwm0ATzjZ1ZgXaVrTg6TkJKblwO8oRo= MIME-Version: 1.0 Received: by 10.216.24.141 with SMTP id x13mr3055785wex.106.1246534389051; Thu, 02 Jul 2009 04:33:09 -0700 (PDT) In-Reply-To: <4A4C61A1.1090602@zend.com> References: <4A4C61A1.1090602@zend.com> Date: Thu, 2 Jul 2009 13:33:09 +0200 Message-ID: <8637af910907020433g592d342am91277c75828554f1@mail.gmail.com> To: Stanislav Malyshev Cc: Ilia Alshanetsky , PHP Internals Content-Type: multipart/alternative; boundary=0016e6dbe88975b054046db7670a Subject: Re: [PHP-DEV] Re: Flexible type hinting From: piccoloprincipeazzurro@gmail.com (Giorgio Sironi) --0016e6dbe88975b054046db7670a Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit I'm an advocate for introducing optional type hinting as I write many assert('is_*($foo)') a day. On Thu, Jul 2, 2009 at 9:28 AM, Stanislav Malyshev wrote: > And there's only two ways to do it - either make absolutely all functions > and variables that interact with typehinted functions to be strict-typed > (which we don't plan do) or do casts on each call to hinted function. I do > not see how anything else could produce robust code provided that type > mismatch is a fatal error and variables can not carry type. In an application not all layers have to deal with arbitrary parameters, only the layer with user input does. Do I write a (Traversable) cast before passing an object to foreach()? No, because the object is not user input. My application will parse POST, GET, COOKIE parameters in layer 1 and call the underlying layer 2 with the right data type; down from the layer 2 to 4, 5, 10 layer I can use type hinting. Type hints became part of the contract of layer N: if there is a runtime error the problem is in layer N-1 not respecting the contract. function generate_numbers(int $maximum) { return ..... } function presentation_layer_display_numbers($input) { if (isset($input['max']) and is_numeric($input['max'])) { foreach (generate_numbers($input['max']) as $i) { echo "

$i

"; } } else { echo $formCodeWithHighlightedErrors; } } presentation_layer_display_numbers($_POST); Validating input it's something we should already do, but the other parts of the code can use type hinting. If I really pass a type that is not right to generate_numbers(), it will be a very bad thing because the function will not know how to recover; and it should not do: it's a job for the top layer. Also, looking at the patch I think it doesn't cover the matter of inheriting > the typehinted methods - i.e. if there's a typehinted method, could I > override it with non-typehinted version or vice versa? What about typehinted > interfaces? For the Liskov Substitution Principle if B extends A you should be able to call B as if it were A. So typehinted methods should be overriden respecting the typehint to allow polymorphism. interface Calculator { public function sum(int $a, int $b); } class ScientificCalculator { public function sum(int $a, int $b); } class BrokenCalculator { public function sum(int $a, string $b); // some error is thrown } class AnotherBrokenCalculator { public function sum(int $a, $b); // some error is thrown } -- Giorgio Sironi Piccolo Principe & Ossigeno Scripter http://ossigeno.sourceforge.net --0016e6dbe88975b054046db7670a--