Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109275 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78809 invoked from network); 24 Mar 2020 16:22:48 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Mar 2020 16:22:48 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 147611804E0 for ; Tue, 24 Mar 2020 07:47:13 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-io1-f43.google.com (mail-io1-f43.google.com [209.85.166.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 24 Mar 2020 07:47:12 -0700 (PDT) Received: by mail-io1-f43.google.com with SMTP id h8so18359817iob.2 for ; Tue, 24 Mar 2020 07:47:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=14T8/7T0cK+f00+fALDqTK4+ILdMw9zn6wNpHRe0+xE=; b=AyAOgav/2wLSHf4w6Q2dFnG9NdbXovqZsHD6g181T5NaiVJkk/iamZWK8heNygCBiZ NW9JfhBZ1rGKAoLtQkqAf0t9QaElLbus8xBDSNZnn7zFz3qol58wbEMe2yWDMp/ZOhIg cotOqWp3ir2mLcUrK6r9UPuppbnwnAtuXvARU6FdCXr3xpvrYvHVS++mfdK+dllZEIK/ E2/RFbBkEEi5VXBBSa1/sgfmQSfKQtB9K68Rzu63FR7DRJ1A11LK6Q2+hdNh9eRbhhRA TL2Z1gsuTE0+Q6ur48qq2+KYZegaxFABLduBXZlpkj451otS3Rjd67cl9oeXSHtfa2t6 hO1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=14T8/7T0cK+f00+fALDqTK4+ILdMw9zn6wNpHRe0+xE=; b=qv29z4NVuTiM4ntOyu5Kkmlz9dPehNYyrIaU0AXawx/8aDwCd5Fv0m9I34uVWyXZBQ ghxpXAS7rAym7A5Xvipt+Y2c3+q7ODghlmsjX+XfjzOJmfIBmkvlQtbT21vz4HNiuk6W a525/NwCh099jVYQjk+oZH8eWK1zTt+623DqoViqsHSY2EH1ZYLWwKgeLhgRhunGMtiC gA4E7AgjHo/LZQr7XZ8ZMEs86bMXhVpCpyiOiDYvnBXAy7BWo9oZJY7pFRnNe7/vd/UI KklAZUCvtVIBrRb5JDSeheKKdOGcLzPkjEVn0mD1uoVa2qjDciI9gANr61f0v69birdb US4g== X-Gm-Message-State: ANhLgQ2I9rXabinwjzf5A/QNDB7HjvCV6BTpjzBShJKc04C74Dyewi7g oUfH6i5FvSYMgixLKapkdQC8wA+np2Nzv+A/yC3+jwJJ X-Google-Smtp-Source: ADFU+vtgTBdzvkPme8MbV/hP0XJbhaNiBho3uKqO6megfDsYZsB+xH7lLuKJVWe2jJbaIysGtO1DDclgI6rGj6/+5vA= X-Received: by 2002:a6b:c408:: with SMTP id y8mr7145969ioa.12.1585061230085; Tue, 24 Mar 2020 07:47:10 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 24 Mar 2020 14:46:58 +0000 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="000000000000910dc405a19ad3e4" Subject: Re: [PHP-DEV] Type casting while array destructuring From: rowan.collins@gmail.com (Rowan Tommins) --000000000000910dc405a19ad3e4 Content-Type: text/plain; charset="UTF-8" On Tue, 24 Mar 2020 at 14:28, Reindl Harald wrote: > > Am 24.03.20 um 15:23 schrieb Rowan Tommins: > > On the other hand, this is exactly the kind of thing where > strict_types=1 makes things worse - you'll actually get *better* error > output if you use strict_types=0 and pass the string to a function marked > as requiring int > no! > > with strict_types=0 the casting simp,y happens by the caller and you get > no error at all > Only if the string is a valid integer; compare https://3v4l.org/uvPYZ with https://3v4l.org/h1aT5 function foo(int $x) { var_dump($x); } declare(strict_types=1); $a = 'hello'; $a = (int)$a; // cast doesn't produce any errors foo($a); // dumps int(0) declare(strict_types=0); $a = 'hello'; foo($a); // TypeError: Argument 1 passed to foo() must be of the type int, string given That's what I mean about "stricter casting" - (int)$a basically always succeeds, so it would be useful to have a version that rejects things like non-integer strings. That could be Yet Another Runtime Mode using declare(), but it could just be a different syntax, so that you'd write this: // regardless of which strict_types mode you're in $a = 'hello'; $a = (int!)$a; // TypeError: string value is not valid for strict cast to type int foo($a); // statement never reached Regards, -- Rowan Tommins [IMSoP] --000000000000910dc405a19ad3e4--