Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:83888 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 83374 invoked from network); 26 Feb 2015 11:24:22 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Feb 2015 11:24:22 -0000 Authentication-Results: pb1.pair.com smtp.mail=dmitry@zend.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=dmitry@zend.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 209.85.220.169 as permitted sender) X-PHP-List-Original-Sender: dmitry@zend.com X-Host-Fingerprint: 209.85.220.169 mail-vc0-f169.google.com Received: from [209.85.220.169] ([209.85.220.169:62399] helo=mail-vc0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DF/53-65287-5620FE45 for ; Thu, 26 Feb 2015 06:24:21 -0500 Received: by mail-vc0-f169.google.com with SMTP id kv19so3689865vcb.0 for ; Thu, 26 Feb 2015 03:24:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=4hmLUUbzi3VxWMfwdsF7X0irDX3FUXWcEYKpd9VaXOw=; b=JTQqTUheeVssHepe+eXsZSBrPFUyhl0pb8QLcbJsKxh3XgFk6O9W2mbKmeZoyI0L83 laYba4I6QFcMWvAV0q5NNPcGDwMsuAZrzpvmLW4eqbrMEvwHufF59VpYJZFO1R+ShCyr KCp6w2C8vfudSw0iZsIC5ZP+gmjrklnML8kYLVVgv3HULbf6EFHZriYMyZEjAMx49Lqg bGb307+LPh4R2KlSt8A+/bF+s7sNQI5Tyo2D5eHpBlk0q7EMAqMp8N5Ogzblh52xV7lS KcUvaw60fQ0AAOvmXh+IF8h8F+i2LoWhtHHASG/2BHDNS9/Jsu+Y+ReT37pmmLJm/RTW +3tQ== X-Gm-Message-State: ALoCoQmUMZUarkkitBg9j5DxN0kGM1P9m667EqQO7VUC+ZCOT+ggNOWcO0JR226VfmDGJ3J2fCzkKrVY9vpuOQ4Swc922j7T5r3MpjEkqmXJ9JpaJb7pcWNhHIwG+AH6gloNmDhdRcPNAEjDs/XJoNr/1q18JtiOcA== MIME-Version: 1.0 X-Received: by 10.52.103.75 with SMTP id fu11mr8179545vdb.5.1424949858737; Thu, 26 Feb 2015 03:24:18 -0800 (PST) Received: by 10.52.113.231 with HTTP; Thu, 26 Feb 2015 03:24:18 -0800 (PST) In-Reply-To: References: Date: Thu, 26 Feb 2015 15:24:18 +0400 Message-ID: To: Benjamin Eberlei Cc: Anthony Ferrara , PHP Internals Content-Type: multipart/alternative; boundary=047d7b86d990273cc6050ffbfe23 Subject: Re: [PHP-DEV] Strict typing and callback vs declare() From: dmitry@zend.com (Dmitry Stogov) --047d7b86d990273cc6050ffbfe23 Content-Type: text/plain; charset=UTF-8 On Thu, Feb 26, 2015 at 2:09 PM, Benjamin Eberlei wrote: > > > On Thu, Feb 26, 2015 at 11:56 AM, Dmitry Stogov wrote: > >> >> >> On Thu, Feb 26, 2015 at 1:34 PM, Benjamin Eberlei >> wrote: >> >>> >>> >>> On Thu, Feb 26, 2015 at 11:10 AM, Dmitry Stogov wrote: >>> >>>> Hi Anthony, >>>> >>>> What do you think about using a user level callback for strict type >>>> checks >>>> instead of declare(). It won't allow changing behavior per file, but >>>> this >>>> has its own cons and pros. >>>> >>>> >>> set_strict_type_checker(function ($class_name, $function_nume, $arg_num, >>>> $expected_type, $value, $file, $line) { >>>> ... >>>> return false; >>>> }); >>>> include("orig_index.php"); >>>> ?> >>>> >>>> If callback is not set, arguments are converted according to standard >>>> rules, if set and returns false - fatal error or exception is thrown. >>>> >>>> The implementation should be simpler and more efficient than using >>>> declare(). >>>> >>>> Thanks. Dmitry. >>>> >>> >>> This ruins portability with third party libraries completely. >>> >> >> Not completely, because checker may be smart enough to return "true" for >> third party files. >> >> > set_strict_type_checker(function ($class_name, $function_nume, >> $arg_num,$expected_type, $value, $file, $line) { >> if (!my_own_file($filename)) { >> return true; >> } >> ... >> return false; >> }); >> include("index.php"); >> ?> >> >> And you won't have to modify each file in your project adding >> declare(strict_types=1). >> > > Yes, but you need a mechanism for each third party library to register > their typechecker code and then build a generic type checker system using > the right checks for the right library. This will produce really slow code > considering it will trigger this on every argument. > Only for strictly wrong arguments. > Also i find declare(strict_types=1) is already adding another stack in my > mind to think about, now having to think about every file/lirary having a > different kind of validation makes it even more complicated. > You 'll have to think about each file anyway. To add or not to add declare(strict_types=1). Callback would allow to care about strict type checks in one separate place. It's like to keep a screw key with every nut in your car instead of keeping toolbox. > > Additionally it destroys the AOT compile benefit of static type hints, > since you cannot compile code down to C again, because the > conversion/validation is not necesarily deterministic. > Oh god... If you know the type of passed and expected argument at compile time, strictness doesn't make any difference (you may only report a error at compile time). If you don't know type of passed or expected argument at compile time, you'll have to check their equality at run-time anyway. Thanks. Dmitry. > > >> >> Thanks. Dmitry. >> >> >> > --047d7b86d990273cc6050ffbfe23--