Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114698 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 732 invoked from network); 2 Jun 2021 22:46:09 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Jun 2021 22:46:09 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 54F93180211 for ; Wed, 2 Jun 2021 15:59:28 -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.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) (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, 2 Jun 2021 15:59:27 -0700 (PDT) Received: by mail-qk1-f181.google.com with SMTP id i68so596214qke.3 for ; Wed, 02 Jun 2021 15:59:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=Zn7per3gVCS+1EK/J8RHswQvDeeTQjuXIIMYg/GEQ8k=; b=ljtxuggRfM6u0VOQqnngptmISi1FIgDPAAGoLm1ussD1GQtIJNOg1dhB5FE364Q4fX soJCVg0w/dLF2KFBZ56/Kf+7cn2bWnIniBY1vVPw0mq32vo3PYMNtgUsSzFMXkxKOPzU soqgy/e1475ing7NFvmz4ekbBWIaUw6No+F8gLNUyVRVi3bkKRcI2SGdyxlUiTnj2N42 IjxQn4pnrv8UNVyIPq5eAgtYKo6LSVTAZM+l3z6dxwYDq3d44MgMSQCEhlUqryWa3R6s THhmWItkCa3AbETZ5/or7J+Jsz5jca//j7ggjdTtuAFc1W0a7Fub6Zqa7rf96DWC3e8w goMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=Zn7per3gVCS+1EK/J8RHswQvDeeTQjuXIIMYg/GEQ8k=; b=BjD+C7s3j1BFe93fxiD+y3cGql9T0xnuxM6VIR2ypBmpA74a0+spjNfHl7KmTriXuk bTXPtfwu+YgqtBMrnTVxX/lGavRVs7L2/Z+lfa5hwAFhF4ME/v7pSh0+0pJksQ0Pzhb5 6+20eQOILbr15a0Eu2KklhI164KbNhiQcTstTZI/g4hhosYaCCLK8x0Fv4TN7L7acv8K Jgislr3IDoBEWDg87rnag4SsUJlZGPNNA5fUXyawJxC0hQaUoU0MF87FfKy2/PiKPGRH FvEk73V2+rg0CyMAOWtO6wwgrG/e59Z9XmMQUdLHoeHZOF7cwbzicbktAVAWCRYRK3qJ qCQw== X-Gm-Message-State: AOAM532HuHv2Y2VpUJyRRCOPDF8iFwVyQtC8pdsKDo2MLwZjoLNOVzg4 msFOXndmRVejx3osKw9e/Qn9r4LSqBOwm85h X-Google-Smtp-Source: ABdhPJz6jpCcymr6bN+hXozckXKSTBgsDTGGBFRbM02U+xx0CQqR9o8Vfbe4XnX+O26uBMnmh5EO2Q== X-Received: by 2002:a37:9d81:: with SMTP id g123mr29588162qke.280.1622674765196; Wed, 02 Jun 2021 15:59:25 -0700 (PDT) Received: from [192.168.1.10] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id 187sm848855qkn.43.2021.06.02.15.59.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 02 Jun 2021 15:59:21 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.6\)) In-Reply-To: Date: Wed, 2 Jun 2021 18:59:18 -0400 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <761D99C1-4728-4EE8-95F8-44A240AE1473@newclarity.net> References: To: Jordi Boggiano X-Mailer: Apple Mail (2.3608.120.23.2.6) Subject: Re: [PHP-DEV] Regarding array_shift()/array_unshift() From: mike@newclarity.net (Mike Schinkel) > On Jun 2, 2021, at 4:25 AM, Jordi Boggiano wrote: >=20 > On 02/06/2021 00:34, Mike Schinkel wrote: >> But from all this I do agree with you that just returning an array = would likely be acceptable. >> ----- >>=20 >> This would not be a great solution for really large arrays, but then = we can't eliminate the need for a developer to have a reasonable level = of knowledge, right? So: >>=20 >> $unshifted =3D $array->unshift($new_element); >> $shifted =3D $array->shift(); >>=20 >> But, we could also possibly use better names for these: >>=20 >> $right_shifted =3D $array->shift_right(...$new_element(s)); >> $left_shifted =3D $array->shift_left([element_count]); >=20 > IMO for unshift() it'd be fine to return a new array, but when = processing a list of things in a FIFO pattern I often used array_shift() = to get the first "job" out of the array. If $array->shift() returns a = new array then how do I access the first item? That is a really excellent point, something I did not think to consider = given how rarely I use array_shift(). =20 But your comment causes me to ponder a number of follow up questions, = some of which are tangential. If any reader feels these tangents are = worth discussing please make another email and quote my relevant = comments so we can have a dedicated thread for each. 1.) Given Nikita's position that it would only be viable to offer a = syntax that simulates method calling for arrays if the methods = themselves are immutable can you envision a solution for allowing = $array->shift() functionality that would address getting both element = and shifted array without resorting to by-reference parameters? 2.) One approach I can envision is to add functionality to PHP that = would allow the returning multiple values from a function or method =E2=80= =94 NOT to be confused with returning an array that can be destructured = with list() or []. There is certainly precedence in other languages to = prove the validity of the programming model, and they solve some thorny = issues quite nicely.=20 I have wanted to bring this up on the list for eons but have feared not = having my use-cases written up well enough to keep people from = immediately having a negative reaction and thus prejudicing too many on = the list for it ever to be viable. But I am going to risk it and hope = this use-case is sufficient to peak people's interest, or at least not = have them summarily dismiss the idea with prejudice. If PHP were to allow multiple returns then we could have one of the = following (not sure which would be best): $element, $shifted =3D $array->shift(); OR $shifted, $element =3D $array->shift(); Following the lead of another language, we could use an underscore = placeholder to indicate not to capture the return value if we don't need = it: $element, _ =3D $array->shift(); _, $shifted =3D $array->shift(); Again, this is NOT the same as array destructuring where the function = would need to return an array containing the array. 3.) When you have used an array to contain a list of "jobs", are you = creating that array in your code or have you gotten the array from a = function within PHP that provides the array such as scandir() or glob()? 4.) Given Nikita's comments on the O() performance of array_shift() = have you considered instead simulating a FIFO queue using an array as a = stack? Any reason that would not work for your needs? 5.) Alternately given Nikita's comments on array_shift() have you = considered using SqlQueue: https://www.php.net/manual/en/book.spl.php? = It seems a perfect fit for your FIFO use-case to the extent you've = explained it. Honest question, not a challenge. Wouldn't that work = better than an array? 6.) More broadly, this made me wonder about the SPL data structure = classes in general. I honestly did not think of SplQueue at first = because I so rarely use any of the SPL classes. I was not until I = started to ponder what "different data-structure or construction = approach" might be appropriate, referencing Nikita's comment. Then I = wondered why I don't think to use those classes more often, and I came = up with these hypotheses, in ascending order of likelihood in my = opinion: A.) Admittedly superficial but the Spl class names make code look more = complicated. I'd much rather see Queue than SplQueue, or more = specifically =E2=80=94 while ignoring the still-raging debate over the = best way tonamespace built-in PHP classes =E2=80=94 I'd much rather use = an \SPL\Queue than an SplQueue. Maybe PHP could create aliases for these = classes but within a namespace? B.) Out-of-sight, out-of-mind. I rarely see people use the SPL = functions in open-source code, discussed on StackExchange, written about = in articles or on mailing lists. C1.) The SPL data structure classes do not have anywhere near the = functionality that arrays have built-in functions for, such as: = array_combine(), array_fill(), array_map(), array_reduce(), = array_slice(), array_splice(), array_search(), array_filter(), = array_walk(), etc. etc. Maybe PHP could add more of that kind of = functionality for these data structures? C2.) Also there are no performant ways to convert one related data = structure to another. You can't easily convert a SplStack to a = SplQueue, or a SqlQueue to a SplPriorityQueue, and there may be other = appropriate conversions that PHP obviously does not provide. D.) Few if any built-in PHP functions or methods accept instances of = these classes as parameters, nor return instances of these classes. = Examples include: sort(), file(), glob(), scandir(), = iterator_to_array(), str_split(), str_replace(), preg_match_all(), = str_getcsv(), fputcsv()/fgetcsv(), func_get_args(), = call_user_func_array(), implode()/explode(), sprintf(), = file_put_contents(), var_dump(), var_export(), filter_input_array(), = mysql_fetch_row(), get_defined_constants(), get_defined_vars(), = debug_backtrace(), etc. etc. So if you are always being handed an = array, no wonder you rarely ever use the SPL classes, right? If we were to address any or all of A-to-D maybe PHP developers would be = more inclined to use different data-structure and/or construction = approaches that better fit their use-cases? So in summary the tangential topics I addressed were: 1.) PHP potentially supporting multiple return values, and=20 2.) Renewed focus on SPL data structures. Again, if anyone is interested in these tangential topics and wants to = discuss further, please create a new thread and quote my applicable = comments from this email. -Mike