Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112884 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 30018 invoked from network); 14 Jan 2021 18:46:42 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Jan 2021 18:46:42 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 86FEA1804E3 for ; Thu, 14 Jan 2021 10:25:12 -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=1.0 required=5.0 tests=BAYES_05,BODY_8BITS, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail.apserver.co.uk (server2.alteredperspective.co.uk [85.119.82.103]) by php-smtp4.php.net (Postfix) with ESMTP for ; Thu, 14 Jan 2021 10:25:11 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by mail.apserver.co.uk (Postfix) with ESMTP id 404544F4064; Thu, 14 Jan 2021 18:24:24 +0000 (GMT) Received: from mail.apserver.co.uk ([127.0.0.1]) by localhost (server2.alteredperspective.co.uk [127.0.0.1]) (amavisd-new, port 10024) with LMTP id qpufjl7iX4nd; Thu, 14 Jan 2021 18:24:22 +0000 (GMT) Received: from [192.168.0.7] (cpc113422-maid7-2-0-cust493.20-1.cable.virginm.net [86.18.185.238]) by mail.apserver.co.uk (Postfix) with ESMTPA id 774B24F4061; Thu, 14 Jan 2021 18:24:22 +0000 (GMT) To: Andrew Brown , PHP internals References: Message-ID: <31e07ff9-3573-cd55-188d-5ec9f99071ff@allenjb.me.uk> Date: Thu, 14 Jan 2021 18:25:05 +0000 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-GB Subject: Re: [PHP-DEV] Feature Proposal From: php.lists@allenjb.me.uk (AllenJB) Hi, It looks like your images have broken (Random guess: the list may remove attachments). As a general rule, I would suggest avoiding screenshots for code. Common mailing list etiquette for development lists is to avoid attachments or embedded remote images as these tend to get lost in history, and not all list members may be using graphical clients. Looking at what I can see of your proposal, PHP already supports default values for arguments: https://www.php.net/manual/en/functions.arguments.php#functions.arguments.default ...and as of PHP 8 this can be combined with named arguments to allow "skipping" when calling: https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments https://3v4l.org/rUGuP     function test($foo, $bar = "bardefault", $qux = "quxdefault")     {         return [$foo, $bar, $qux];     }     var_dump(test(foo: "foovalue", qux: "quxvalue")); This doesn't allow for declaring required parameters after optional ones in the functional declaration, but does allow for flexibility when calling while making it explicit which parameters you actually want to pass. While some time ago, this feature suggestion has been discussed before: * https://externals.io/message/105123 * https://externals.io/message/80201 (There were additional older threads I found via externals.io searches for "default keyword" and "parameter skip" which I've not included here) You may also want to check threads / the RFC related to named parameters as there may be additional discussion there. AllenJB On 14/01/2021 17:52, Andrew Brown wrote: > This is my first foray into PHP internals, so please let me know if > I'm doing something wrong. Currently just following the instructions > from https://wiki.php.net/rfc/howto . > > Currently this proposal is only a "concept". > > I propose we add a "default" keyword that can be passed as an argument > into a function/method call. This would allow the calling code to > defer to the function signature for the value of a parameter. > > Traditionally, all optional parameters are placed towards the end of > the function parameter list, but as we all know, sometimes this order > can be tricky. So if some calling code wants to override a later > parameter, it currently needs to pass a value to the earlier optional > parameters. > > A current solution around this is to define all defaults in the body > of the function rather than in the signature. > > Screen-Shot-2021-01-14-at-11.45.09-AM.jpg > > However, this adds some extra boilerplate, and we can't use default > parameters as they were really intended. > > My proposal is to add a new `default` keyword to be passed as an argument. > > Screen-Shot-2021-01-14-at-11.44.57-AM.jpg > > The first call of the function here is how we currently have to do > things. We have to explicitly pass `true` in as the second parameter. > > However, in the second call, we now use the new `default` keyword, > which will defer to the function signature for the default value of > `$param2`. > > Thanks all, looking forward to some feedback! > > -- > Andrew M. Brown