Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98750 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 73897 invoked from network); 9 Apr 2017 11:07:09 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Apr 2017 11:07:09 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.174 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.128.174 mail-wr0-f174.google.com Received: from [209.85.128.174] ([209.85.128.174:35015] helo=mail-wr0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EE/33-08240-BD51AE85 for ; Sun, 09 Apr 2017 07:07:09 -0400 Received: by mail-wr0-f174.google.com with SMTP id o21so104069854wrb.2 for ; Sun, 09 Apr 2017 04:07:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:user-agent:in-reply-to:references:mime-version :content-transfer-encoding:subject:to:from:message-id; bh=xRYbmgIpCNFP485VUs8hHXzgJNtroGvhRoI4E/IdB4w=; b=tdtho16OJpBx0Lq3iR1lRuD7GZu8lTSLBQMHJzvEODBlcFQEd64krwynXWsTe6b9ko W4bCGwYHzgdq88kcWITe5y/7qsq+bGSccx32xeLMyI7M4NE+jXA8MMt0TQ8i3hrmi7ZE 8Qu/UMho+ZETE0TaOTekF/3wE1r05Ug1BKXyJe4BQtnfyksTPUhoB5TTihmubrLAZvRv EwexlRY1YViLaaVvaeiywrV0i7XrmVgRE6cPLsWpQNpohwH/PDr3Ai6jS8e5/FGL4pzF iWlXWBWSSxr8TfFChNJpaCdi1eKoJXg13W4zt6RSnI7ze/DFD//qp0uj/zVTobn/tjXA ef4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:user-agent:in-reply-to:references :mime-version:content-transfer-encoding:subject:to:from:message-id; bh=xRYbmgIpCNFP485VUs8hHXzgJNtroGvhRoI4E/IdB4w=; b=iPG32aFYROjhWjdxrhauBukaF/tQ/VTS+SkjQ0Y64r6Z0CspEL7vzwgVCOJi1FnBqs wWbsHK1AVHb4F330dQp4Inqm7tv8rvnykOvPhW6phrNeI+eSjDCypWSreTz45PJn0K+5 nzomcRTaUzMc4qDMaKRMzU1hcSgGjPBkUR9pbMqxdgr1cY6iZ3wXHqDMLjdtlG9jAMz0 XjIK+/JdvAEuovazmHXXRaaoNWUNlYZBrj+sxkB8Jo+atLEYciqWHkU7kcIGEdkxm/43 hnISTnMVeK+TQ9XWUrrXjER87mn81hGb93/LerFak7DsoKw+iDll+LW3L4S67Cw+F3cn 19jg== X-Gm-Message-State: AFeK/H0Ojo+g3McqMgAveyjHFwMxWVC/NqbEaqynrb3ZZNOtPqFXopw3IwSeUUfTG+lKPA== X-Received: by 10.223.134.152 with SMTP id 24mr35368252wrx.30.1491736025148; Sun, 09 Apr 2017 04:07:05 -0700 (PDT) Received: from ?IPv6:2a00:23c4:4bd2:6e00:e803:3eca:a09c:32e1? ([2a00:23c4:4bd2:6e00:e803:3eca:a09c:32e1]) by smtp.gmail.com with ESMTPSA id e23sm6020253wmi.16.2017.04.09.04.07.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 09 Apr 2017 04:07:03 -0700 (PDT) Date: Sun, 09 Apr 2017 12:07:02 +0100 User-Agent: K-9 Mail for Android In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable To: internals@lists.php.net,Rasmus Schultz ,PHP internals Message-ID: <876C54BA-B07F-4468-8709-36C495D04311@gmail.com> Subject: Re: [PHP-DEV] scalar type-casting From: rowan.collins@gmail.com (Rowan Collins) 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 =2E=3D "_bar"; } >$foo =3D 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=20 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=2E If the calle= r sees only the signature, then the function can do *literally anything* to= their passed by reference variable=2E The caller is giving full control an= d "ownership" of that variable, and shouldn't make any assumptions about wh= at it will look like when it comes back=2E For example, you don't even need PHP7 to do this: class Foo { public function __toString() { return "foo"; } } function append_to(&$str) { $str =3D (string)$str =2E "_bar"; } $foo =3D 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 chan= ge type in the parameter handling of your example function (with strict_typ= es off) than inside the body of my example function=2E Of course, by settin= g strict_types=3D1, 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 exa= mple, though=2E Regards, --=20 Rowan Collins [IMSoP]