Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:83786 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 50625 invoked from network); 25 Feb 2015 13:54:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Feb 2015 13:54:25 -0000 Authentication-Results: pb1.pair.com header.from=dmitry@zend.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=dmitry@zend.com; spf=pass; 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:39279] helo=mail-vc0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DE/E1-62407-F04DDE45 for ; Wed, 25 Feb 2015 08:54:24 -0500 Received: by mail-vc0-f169.google.com with SMTP id kv19so1386581vcb.0 for ; Wed, 25 Feb 2015 05:54:21 -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=YEg2vQ9m97mAns5EU2dCP5CEMIVnifVQJjxbrwCyDtI=; b=BCq84Tk9z4+Y88k42yMxsvHGnyAaVyxrGzJDtBf09J4pQUeGudtOxZnQDFwHtoFU02 k3VfFeu2ot+Hi+A1b2ODZ/w7rRpZdCOBYAEyqJWJQOrNfFQYA5R+6xCxv+ZwZOBmh8AF mPVWCyP7TQleZ2XMuEkj25HjWph9IzgIwVP11YVd/SulxeZcBLKiWdeNvalM/S2YjTwG ZVoRxZikpUNrDvxnNzPL4QkK3iibdO6Y/v2g3yZxUsxTttJ+o3aq6EPejvZgmSaSpBVD TdyWTV52fl6FagZMhvxFOQvZz1+yOzxAZXyyrlHcfBp1ROImS4do1Z8DHZEeDILcZUIw inWw== X-Gm-Message-State: ALoCoQmMnlO37LybMNSTRAkSU8opwm1JsKJNSQIfbYHynjW/vebC/0Y1hlxCvTm+TaG1spQw34fCe10YY6Tt4OY5SwTNPuGVgRIFGMkkfx1hrtrHtezq+E+6TA9enHF41VluNZC+I42tdTCkA4pG+yTF8Be1vU8IIQ== MIME-Version: 1.0 X-Received: by 10.52.25.11 with SMTP id y11mr3913830vdf.51.1424872461261; Wed, 25 Feb 2015 05:54:21 -0800 (PST) Received: by 10.52.113.231 with HTTP; Wed, 25 Feb 2015 05:54:21 -0800 (PST) In-Reply-To: References: Date: Wed, 25 Feb 2015 17:54:21 +0400 Message-ID: To: Niktia Nefedov Cc: PHP Internals Content-Type: multipart/alternative; boundary=001a1133e5f2e77317050fe9f852 Subject: Re: [PHP-DEV] Re: [RFC-Discuss] Scalar Type Declarations v0.5 From: dmitry@zend.com (Dmitry Stogov) --001a1133e5f2e77317050fe9f852 Content-Type: text/plain; charset=UTF-8 On Wed, Feb 25, 2015 at 3:15 PM, Niktia Nefedov wrote: > On Wed, 25 Feb 2015 16:55:57 +0400, Dmitry Stogov > wrote: > > > > On Wed, Feb 25, 2015 at 2:42 PM, Niktia Nefedov > wrote: > >> On Wed, 25 Feb 2015 16:30:32 +0400, Dmitry Stogov >> wrote: >> >> anyone may tell, what this will print without running :) >>> >>> main.php >>> ======== >>> >> declare(strict_types=1) >>> include "a.php"; >>> include "b.php"; >>> var_dump(foo("5")); >>> ?> >>> >>> a.php >>> ===== >>> >> declare(strict_types=0) >>> function foo(string $a): string { >>> bar($a); >>> return $a; >>> } >>> ?> >>> >>> b.php >>> ===== >>> >> declare(strict_types=1) >>> function bar(int &$a) { >>> var_dump($a); >>> } >>> ?> >>> >>> Thank. Dmitry. >>> >> >> >> Hi Dmitry, >> >> This will error out because $a in the scope of `foo` will be coerced to >> int type when passed to bar by reference. >> > > I think it'll work without errors and produce some explainable but weird > output. I didn't run it myself yet. > > > Yep, you're right. But it's not a problem of mixed-mode, it's more of a > problem of introducing coercive type hints at all, honestly... I wonder > what Zeev plans to do with __toString coercion in his RFC, because that is > even more severe problem when your object unnoticedly becomes a string. > The object on the call-site should remain to be an object (if it's not passed by reference), however the called function will receive a string. It works in PHP-5 and PHP-7. Nothing should be changed. $ sapi/cli/php -r 'class X {function __toString(){return "abc";}} $x=new X; var_dump(strlen($x)); var_dump($x);' int(3) object(X)#1 (0) { } However, declare(strict_types=1) will break this. see https://github.com/ircmaxell/php-src/compare/scalar_type_hints_v5#diff-ef5bf53d1412b50f85d125ca4fe84741R1182 Thanks. Dmitry. --001a1133e5f2e77317050fe9f852--