Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:110735 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 97838 invoked from network); 26 Jun 2020 15:16:52 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 26 Jun 2020 15:16:52 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3D596180088 for ; Fri, 26 Jun 2020 07:04:47 -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, 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-lj1-f179.google.com (mail-lj1-f179.google.com [209.85.208.179]) (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 ; Fri, 26 Jun 2020 07:04:46 -0700 (PDT) Received: by mail-lj1-f179.google.com with SMTP id s9so10427234ljm.11 for ; Fri, 26 Jun 2020 07:04:46 -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 :cc; bh=g4kXDQhPVcyeyMUCID482JudJGiRX6O4oCXbziIU2e4=; b=VUZVs6lZV6LNe9k6JHix7b0St9bvjBJNsqpBA+/Bqs5nY2TLrgJriLqdxfJRoElY6l ugIStvuLiy8oVLyTb7GLLvlME+dqULrRruTcqNdOqfnmvpprwf1C4oXvLlMUeiKjPNml 0krZet9KMbeHFcRdwp7/A73hCCbHL+2sVmKc7AxW+O2Wt3emD2e4euXoXil+Zo/fu5QJ dZNxj/xGVQizuS+OTGPlRU+4RW/pWNbPWZnqcQqyu9o2M/d22ESS+NRp0mLHF86EAkar s8RW964kqoFwU9NRi0F0vkKriMyratoP0HYcDm9rmgEZdZup1F5qzQH4Gc8A2K+aiwqv lXNQ== 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:cc; bh=g4kXDQhPVcyeyMUCID482JudJGiRX6O4oCXbziIU2e4=; b=ZCB8173IRsELkE9s/RsW5/mKmT8srh7TXaKIG8G7tG5hyXpRYerPH6vPczx3lxd4SB ICKYtWm5VcB0I0tvVbJpvZIQPv8DQ0dcM6NcZ12RRfl+FqNgs8WEW0NdysJmHQTTzVLu +uZg6wt52DDu7ssTh907QEHIU0n4yRxl2N6NUfHHnbgx+kkaiNzO5BaF2mQRH0sS271/ TvWHsxosKs4Gi/sXQE00cgc9hKWm4Q3xGCzVXYjM9sTD31KF06n5J01Hkw4wBXPrBCkt GicioItM28JDtZUQYHmcOWPdPditrQNsP4hBF3ZDdzgSPCWL5OGGhQCadol0Vfi7QP8t i92Q== X-Gm-Message-State: AOAM530LP0eEDHB69fuseRJ85vdl0vJPvPjVYDG30OLEWJy/4UzC50LY TFCv4AIPTC4DiUWEHdoS6RVvcBoTjegvtYjrYv8= X-Google-Smtp-Source: ABdhPJzxWrMhW6ZRhRpe7ui3KC52199UmWDbzXeRZf/wpkVkm2tuhbf6QOAEO1F6ky710Ex5EBMprabzTHWNcUGpq+4= X-Received: by 2002:a2e:751a:: with SMTP id q26mr1449898ljc.258.1593180282040; Fri, 26 Jun 2020 07:04:42 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 26 Jun 2020 16:04:30 +0200 Message-ID: To: Levi Morrison Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Convert SplFixedArray to Aggregate? From: alexinbeijing@gmail.com (Alex) Dear Levi, I will add a test case to the PR to ensure that things work properly in the described situation. On Fri, Jun 26, 2020 at 3:57 PM Levi Morrison wrote: > > On Fri, Jun 26, 2020 at 12:45 AM Alex wrote: > > > > Dear PHP Internals, > > > > I would like to propose a backwards-incompatible change to > > SplFixedArray which fixes the strange and almost certainly unintended > > behavior reported in Bug 79404 > > (https://bugs.php.net/bug.php?id=79404). > > > > In short: Because SplFixedArray is an Iterator, and stores its own > > iteration state, it cannot be used in nested foreach loops. If you > > try, the inner loop will overwrite the iteration position of the outer > > loop, so that the outer loop 'thinks' it is finished after the inner > > loop finishes. > > > > To illustrate: > > > > $spl = SplFixedArray::fromArray([0, 1]); > > foreach ($spl as $a) { > > foreach ($spl as $b) { > > echo "$a $b"; > > } > > } > > > > Only prints two lines: > > > > 0 0 > > 0 1 > > > > The fix is to convert SplFixedArray to an IteratorAggregate, so each > > nested foreach loop operates on a different iterator object and thus > > nested loops don't interfere with each other. > > > > Now, it would be possible to do this while still defining key(), > > current(), next(), etc. methods on SplFixedArray. However, that would > > cause an even more insidious BC break, since these methods would no > > longer work as formerly when called in the body of a foreach loop. > > Therefore, I think it better to remove them. This may break a few > > users' code, but in a way which will be easier for them to debug than > > if the old methods were kept but subtly changed in behavior. > > > > Code to implement this change is here: > > https://github.com/php/php-src/pull/5384/files > > > > Your comments will be appreciated, > > Alex Dowad > > > > -- > > PHP Internals - PHP Runtime Development Mailing List > > To unsubscribe, visit: https://www.php.net/unsub.php > > > > As someone who has patches in the SPL, I support the idea of this > change. One gotcha of aggregates is iterator invalidation; are you > sure the patch takes care of it? If you have an array of size 3, get > an iterator, iterate to offset 2, shrink the size with `setSize`, and > use the iterator at offset 2, will everything be okay?