Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98752 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 90392 invoked from network); 9 Apr 2017 17:02:36 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Apr 2017 17:02:36 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 74.125.83.46 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 74.125.83.46 mail-pg0-f46.google.com Received: from [74.125.83.46] ([74.125.83.46:35266] helo=mail-pg0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F9/45-08240-B296AE85 for ; Sun, 09 Apr 2017 13:02:36 -0400 Received: by mail-pg0-f46.google.com with SMTP id 81so94540117pgh.2 for ; Sun, 09 Apr 2017 10:02:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindplay-dk.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=1j92sdbikKmdVjoB/FSVBiWpNkEDAue/TlmGELJBoe4=; b=MUTTQFeQ5jLOC7XXCvuRtVpHv8RvBMKoyTyRYKhBI2k5sOZO9lYskzorFRcVcUtegW f49jTyb1AqKgy35tc6ONZS6e3J1qZYZhA9wV6tWQULUj0U3wB/GwKa29CVV7dcBKSkKr lI63s5iUZrf7BLGpK0ol5gMdsDSOxZVJrPXAYwQNcoqsjvvLmgpcgEY9+Mb0XVYdHBHa Pqcai/Syjk/vF47Z9D7dfD1FAPan2tPb31UpBNcwlywaVYYOQc+/aGJkZvIkXd5A2czF mWZ3YPr/LifnY8mMMTHdOSKoD3WruE10tz9mDAU55JHPXL555eQ6dasnFEvKNNh1w94k X5cA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=1j92sdbikKmdVjoB/FSVBiWpNkEDAue/TlmGELJBoe4=; b=je4xqfNOgeycRW6MJtMjArnNNQpc3JpusvdAyDEndE0SPRegzp7YEH815aZeNis2ez u/19sI+ws+Fzeamfv8rrSHoEDG1KT3V+DN3LYLXzw7f+UunlGh6cN3nDZinFxEylCbGE JlGVS/48vKKXThigM51OkiC872+88DfM2uZZw9SZJxwefxwm92CP3kiW93nZoQ7l/5vI 8vbiJ+2/nNw2LgV8wsIopgPdey/KD2HxXXDY7Sg1GRSVoZGDbCn5cYjkriEKwapji+FY 8kOep/cYaNTs3hlSp1mFkWDPQlkIgE5soYZLp/KTeFE5pXYU2h0XTJUHGXmXAev8nIfo rvow== X-Gm-Message-State: AFeK/H3yazj/R30xWvVl+yTZ+sAwib7gOEz8C40/Q2qHyDalmflmz497IWJ1NWDPrK+T5gxVnSnufN0DoMVl3Q== X-Received: by 10.84.208.102 with SMTP id f35mr64059022plh.19.1491757353045; Sun, 09 Apr 2017 10:02:33 -0700 (PDT) MIME-Version: 1.0 Received: by 10.100.145.150 with HTTP; Sun, 9 Apr 2017 10:02:32 -0700 (PDT) In-Reply-To: <876C54BA-B07F-4468-8709-36C495D04311@gmail.com> References: <876C54BA-B07F-4468-8709-36C495D04311@gmail.com> Date: Sun, 9 Apr 2017 19:02:32 +0200 Message-ID: To: Rowan Collins Cc: PHP internals Content-Type: multipart/alternative; boundary=94eb2c1482241ea4f7054cbed314 Subject: Re: [PHP-DEV] scalar type-casting From: rasmus@mindplay.dk (Rasmus Schultz) --94eb2c1482241ea4f7054cbed314 Content-Type: text/plain; charset=UTF-8 > If the caller sees only the signature, then the function can do *literally anything* to their passed by reference variable. The caller is giving full control and "ownership" of that variable, and shouldn't make any assumptions about what it will look like when it comes back. Understood, only this isn't the function doing something - it's the language. But I guess the best thing under any circumstances is to simply avoid using references entirely - you won't have these problems then. The main point was, I'd still very much like to see Anthony's RFC revived :-) So I guess this should be updated: "when an internal function accepts a typed parameter by reference, if the magic cast method is defined on the passed in object, an error is raised as the cast cannot be performed because of the reference." Perhaps to: "when a function receives a type-hinted parameter by reference, if the magic cast method is defined on the passed in object, the variable in the calling scope is immediately overwritten with the result of the type-cast." I don't happen to like it, but that's consistent with the existing behavior, right? On Sun, Apr 9, 2017 at 1:07 PM, Rowan Collins wrote: > On 9 April 2017 10:30:02 BST, Rasmus Schultz wrote: > >Example: > > > >class Foo { public function __toString() { return "foo"; } } > >function append_to(string &$str) { $str .= "_bar"; } > >$foo = new Foo(); > >append_to($foo); > >var_dump($foo); // string(7) "foo_bar" > > > >In this example, the caller's instance of Foo gets wiped out and > >replaced > >by a string > > While this looks surprising in the form you've written it, it should only > really be a surprise to the function author, not the caller. If the caller > sees only the signature, then the function can do *literally anything* to > their passed by reference variable. The caller is giving full control and > "ownership" of that variable, and shouldn't make any assumptions about what > it will look like when it comes back. > > For example, you don't even need PHP7 to do this: > > class Foo { public function __toString() { return "foo"; } } > function append_to(&$str) { $str = (string)$str . "_bar"; } > $foo = new Foo(); > append_to($foo); > var_dump($foo); // string(7) "foo_bar" > > I don't think it's any more unreasonable for a reference parameter to > change type in the parameter handling of your example function (with > strict_types off) than inside the body of my example function. Of course, > by setting strict_types=1, the caller can change the implicit cast to an > implicit assertion, and get an error in your example; it won't save them > from my example, though. > > Regards, > > -- > Rowan Collins > [IMSoP] > --94eb2c1482241ea4f7054cbed314--