Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109270 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 51268 invoked from network); 24 Mar 2020 15:08:17 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Mar 2020 15:08:17 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1CBB51804C6 for ; Tue, 24 Mar 2020 06:32:43 -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=-1.3 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8560 212.227.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from mout.web.de (mout.web.de [212.227.17.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 24 Mar 2020 06:32:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1585056756; bh=tRUZf8mNptIv8mdASuk8h61uDoZUm2R+dvjPFLMhv0Q=; h=X-UI-Sender-Class:From:Subject:To:Date; b=KjmRPp959TsZqA/enMGXhfIWZH35VNc6ssUbElahBY5J+uEs9FleZv/lwafbmJYqQ Ty0if2C9eqSx4yJA46/p3Dlgii+rXHU+dywllXJPNEon47o086zwsDzCtdcGHhPL3J WAgxoQauz6d3cvNG2xvTl3zGWOGhRhgIHOLWRlO4= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Received: from [192.168.178.21] ([90.186.125.58]) by smtp.web.de (mrweb102 [213.165.67.124]) with ESMTPSA (Nemesis) id 0Lvjn6-1jKHua0Bmm-017Xoz for ; Tue, 24 Mar 2020 14:32:36 +0100 To: internals@lists.php.net Message-ID: Date: Tue, 24 Mar 2020 14:32:35 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Antivirus: Avast (VPS 200324-0, 24.03.2020), Outbound message X-Antivirus-Status: Clean X-Provags-ID: V03:K1:Cp7pGv+C0mYzG1y02Q115pdy0pKV2L7lVdu7DpOL2QNQiA2Q41i ELxYJI6Dw+/NpNmjHLFrCIymgH62ocaafPNlelh3PNXmXT821PhmquhfnLAuTAK0i16n0A9 WbCQbZthNpBw5BnvmGe271KgkQfFHrIQoOzBE3Vyc4HpQ0f0SERfCQrdETm8AOa8ebFPNYN RqpQ2NyobWgOsTSs+ztGQ== X-UI-Out-Filterresults: notjunk:1;V03:K0:CweUWKEwwf8=:mFOJmhsezmglqdQ1YSQHBk w5zecul7Wbd76gDKXRbMr32Rf04G4KHkERa34xAoyrLnFMgcvN5J7Gq9WXnQmCf7PxBDxbn2f x5TQGvD74oIT1Ifbbj6x2sqYV1Q6VrjRNXhbNvfcImIja+dV7zOcrJx7YXelLAA9Gbha2KNkT C7MBF/EA6HhlzzNfhlSgD5Ino9xIGJ81CzA8z/tV7gr92bJWLb4MudNVEvCp7UEJwTbZMnDP1 2xcwT1GUAI7lEUIj0FxSOo9tnjcVdEAzJq1Lq1Vj54F6GiGRuKVWSXhSusW1IrlZlu0P5i400 s+t0XJxEf2W+jJxS6nlWbL5F5XyNQAaKJRBl5YUXXYGK2j666IUD3ztbaAtOpBjNRSjMSuXA5 zb64JmPV4eTFcTeCxBkkAHkbCbVoktBtysWDANnpHJTBCxe5GaX0BZt+zayj55tIvXM1R4IXV Y7uEbY/n1TGbNjKp1ZQXU/MChDlSiJh5R4Fpt7DwQEpSnafdNfP9liOUedTsdhEy5JD5C2y76 xOpjM1lb5fNxJpjiuHsVIQ/3DoKLtMEK0GwRigKe5NDLjvdXecyqCB2plslKQWBfUY3INl1IA Fsq9ywqOnwb3w9HEO50NsFjjShv8hOVNGpkVXJR3GXTKcjr7arq+M+kCYBFX3lxPn/D7xkGXP WQM7AfuCguRcTDmlSijgH03z/6k70Cnzc9IxF4Uw/pRbyWAfDqae7wNauZ8P/mc0V2aLahTZj eZgY6vbc9HS6krNK95ShrsCCAofnozJsMOPnJALO2DX7eFp6bpIdxnKsCzGXg48GoXtYLbHDw psIEOaNAGY19ef8qTBl+gsGH1bt8PiRWNph9pktH5Gud1cGUnxVwW9zs0RqdwxVJcA7H82uK5 enD0Kftk8YUaluT+6SjxgmKtUzf0Jhjfxk0m0rfkSKWbkyFUK5TngpO9kgm53ZwPItYSyrLK3 Hl49+hxwXu1HhTx0WtW8hhSBG+s+erE9BPI/IhIHlG6HhzvBYYj0v4by7CpQh0iCTuISlzARz RN/1qBBH11UWXiD2l8g8A0j8Hk/G1qW2WqIrIYQ8X+wuqzk6ZEiKcy2ejGrrP6ZUJv9YXK20W 2vbwiui/4r9pLBf3RnQvoeJQjruVh6Bb7ulO5M3cDHr01k0ZaGVetG+dOveiQ+fL+yqAogYdh EP2kGTON1o/cNMI7aILhz4NitjCIzq8GL+w4xbXaeHw0hkS4l507wBPeeAsSt55CGj3j4S99P Ph4uftP0bpwEJEzyx Subject: Type casting while array destructuring From: enno.woortmann@web.de (Enno Woortmann) Hi, currently when using array destructuring the variables are assigned as they are. For example we split a string with explode all variables will contain strings: $data =3D "foo:*:1023:1000::/home/foo:/bin/sh"; [$user, $pass, $uid, $gid, $gecos, $home, $shell] =3D explode(":", $data); If we want to write functions consuming $uid and $gid as integer values with strict types enabled we need to cast the values afterwards: $uid =3D (int) $uid; $gid =3D (int) $gid; // or during the function call $myConsumingObject->myConsumingFunction((int) $uid, (int) $gid); I think you get my point. How about adding some syntactic sugar and allow type casting inside the detructuring expression? $data =3D "foo:*:1023:1000::/home/foo:/bin/sh"; [$user, $pass, (int) $uid, (int) $gid, $gecos, $home, $shell] =3D explode(":", $data); // $uid and $gid are integer values now. All other variables remain as they are and contain strings An example with associative arrays in loops: $array =3D [ =C2=A0=C2=A0=C2=A0 [ =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 'name' =3D> 'a', =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 'id' =3D> '1' =C2=A0=C2=A0=C2=A0 ], =C2=A0=C2=A0=C2=A0 [ =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 'name' =3D> 'b', =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 'id' =3D> '2' =C2=A0=C2=A0=C2=A0 ], ]; foreach ($array as ['id' =3D> (int) $id, 'name' =3D> $name]) { =C2=A0=C2=A0=C2=A0 // $id contains integer values } Further thoughts: when using the list() reference assignment implemented in PHP7.3 the referenced value could be casted (something to discuss about, maybe as future scope as casting a reference assignment currently isn't supported): $array =3D [1, 2]; [(string) $a, (string) &$b] =3D $array; // $a would be a string: '1' // $b would be a string: '2' // $array would contain one integer and one string element: [1, '2'] Thoughts?