Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116737 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 45656 invoked from network); 29 Dec 2021 16:17:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Dec 2021 16:17:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E09EF1804DF for ; Wed, 29 Dec 2021 09:23:29 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,NICE_REPLY_A, 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-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 ; Wed, 29 Dec 2021 09:23:29 -0800 (PST) Received: by mail-wr1-f46.google.com with SMTP id i22so45642719wrb.13 for ; Wed, 29 Dec 2021 09:23:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=MTHk8X763eK31sy2GU3ZtLzLdaO5YdFEfbZ3/dJjOk8=; b=daDDCMtxeJ74ZXga3MmAhN8kB7kEWdcgOG0VbKlo7Wyd3WPHDDIHAc8tlQCJ9vwUdW JU+Unl0fHwHQA2pAkdCUXMQEzsRIt0wptRz8MP7sHhzJtqcKZT2+6SQZnUiP96/TCqOR qFLx/6GIdmlr3GB8Oikp6/lleujzejOUsDmTxOi8GPyjr+h/dUWzTBIF+RVU25UWREGx YkggOZ60MDrh/EXyuIV3RaQ/jg6wC3dFwH/Pqoq0HAsM7NpOqAxlXk7sUjPp4TVueD6w SUWFmsai8s2ahGWpmXpwfI/RFlSCywkkVI669iFR/719NiHgjxJk+2UsakqQH5hEpr5R 5pag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=MTHk8X763eK31sy2GU3ZtLzLdaO5YdFEfbZ3/dJjOk8=; b=y2xri06gKH5cUuj4Xsibsn4OvhPaxY4DdYdsLd4tPpyY3YtpwAVNZHDKhS6JBnDtyz U8+uSs5pfGarL92+zyXL8p//GpJZ69HT80PxVbsFIMw4haVWu0OpvWolJBTAaWqG/oxK w9LP0sSfyR/lJvzXzMRGnGJxlvD/hZKt46TLTFjlXqNhe1zi7L4b1TcQ0lBfurXU9Akq aino9RpZIizEAI0LSlNbkxbUw1fux0BA421/rEw2V+DNq3yIqywhpWlUipBiNgOV0SMQ Z24jj5XbLFNC5z6GblBwITCvrWAzHyJaJcsbhWHyiU9OoxAZiXOnM8zCFC0t67QNrMrU dPYw== X-Gm-Message-State: AOAM532b8H6pKkAFV2ccTFrgI5QrPUutwXe6FfxAWA6rIELNYTm5OLMW MCSDkHY1qbCKGVASZETXmDr2l+afK+w= X-Google-Smtp-Source: ABdhPJyJo/XgPLCvkIE2CoVJIQfCWg/h+ksLg0W18+eBAeE9HElYzpN18KgCq/zVWZLtQDufe6P3+A== X-Received: by 2002:adf:d1e2:: with SMTP id g2mr22400116wrd.362.1640798608172; Wed, 29 Dec 2021 09:23:28 -0800 (PST) Received: from [192.168.0.22] (cpc104104-brig22-2-0-cust548.3-3.cable.virginm.net. [82.10.58.37]) by smtp.googlemail.com with ESMTPSA id j3sm21613225wro.22.2021.12.29.09.23.27 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 29 Dec 2021 09:23:27 -0800 (PST) Message-ID: <5ca1c05f-273e-d1f9-5be0-32eee68a6fe1@gmail.com> Date: Wed, 29 Dec 2021 17:23:24 +0000 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.4.1 Content-Language: en-GB To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] RFC: Stop to automatically cast numeric-string to int when using them as array-key From: rowan.collins@gmail.com (Rowan Tommins) On 29/12/2021 16:58, Lynn wrote: > While I'd love for this inconsistency to go away, I also know that this is > most likely such a big change that it causes months of work and broken code > because it relies on this behavior. It wouldn't surprise me if this > singular change would cause more work than all previous BC breaks combined. I was about to say the same thing - this is one of those ingrained behaviours that is very hard to analyse use of without running the code, so only someone with an extraordinarily good test suite would detect that their code was affected before it went live. Like a lot of PHP's type juggling features, it is surprising in a pure data crunching context, but extremely reasonable in PHP's original and still very popular context of processing input from web forms. For instance, a list of products fetched from a database will likely have indexes which are integers, but a value from $_GET or $_POST will always be a string. It's therefore quite helpful that $products[42] and $products['42'] refer to the same thing, so that you can write $products[$_GET['id']] rather than $products[(int)$_GET['id']]. Would this feature be included in a language designed with knowledge of how PHP is used today? Possibly not, but we have no time machine to change our minds now. One vaguely plausible (but not easy to implement) way forward is to have new array-like types, where keys and values can be constrained in different ways. Then it could obey the local scalar type passing setting ("strict_types") to allow combinations like this: declare(strict_types=0); $stringKeyedArray = new dict; $stringKeyedArray['42'] = true; // key kept as '42' $stringKeyedArray[42] = true; // actually sets key '42' $intKeyedArray = new dict; $intKeyedArray[42] = true; // key kept as 42 $intKeyedArray['42'] = true; // actually sets key 42 declare(strict_types=1); $stringKeyedArray = new dict; $stringKeyedArray['42'] = true; // key kept as '42' $stringKeyedArray[42] = true; // TypeError $intKeyedArray = new dict; $intKeyedArray[42] = true; // key kept as 42 $intKeyedArray['42'] = true; // TypeError Regards, -- Rowan Tommins [IMSoP]