Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115268 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2583 invoked from network); 1 Jul 2021 16:35:03 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 1 Jul 2021 16:35:03 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 470991804DF for ; Thu, 1 Jul 2021 09:55:33 -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,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.42]) (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 ; Thu, 1 Jul 2021 09:55:32 -0700 (PDT) Received: by mail-ot1-f42.google.com with SMTP id r4-20020a0568301204b029047d1030ef5cso261227otp.12 for ; Thu, 01 Jul 2021 09:55:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ralphschindler-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=jpVN3/fPzUPi7skwO8ItTqH0g3haIuhcyMGCSv3qHjg=; b=SzBFM/+Hs5OjAq3yKu2Gp7FOdZmeZGQkzObDsha3QQxbJyUUi9n4JYSAEsXVHzZSzF UwIpaNKqUOmpnltdkDsUh6X1OlFeZEldU77Rxl6jd0rMbk5QDVKvhH+NLkxzcnjaSDur 6P/llwdtn0WWHX3Udjzy38ZWZuLglYn5YztfMe2Q8+1r9/f7n0ikxVv5PERIfAw3bROq djDVk3EVdc5GWmi/thK4rQhUTLVQCtUEaj9vIBJxmKEmL+xnJHjxhty9TJLfJBy+pnEb GfE7ilNxAKVTWE7tvpzQiE5ImgAsCG/w6QJs9R72UmUuNWF7XqLqnruS3CeStJqgZ2ur hSLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=jpVN3/fPzUPi7skwO8ItTqH0g3haIuhcyMGCSv3qHjg=; b=r/BYdUywBauggExjNZQSX7nPUxa6NAnjX12q8zpcPE6ReUuf43A+BM4jjcIWn33ybI El7toUGVNpsICwF7cYRHwJa+QA8jW1NuztaVU4ewbr0GmtsjqCn0LIaOXeI3JgIOR63m GgWjabuQhWIx1+8XCRLC1UZ7Mna07nxXZdhKmB2mM3bjNT58AZW/D6lJHxe65VxJWsF8 QrkDHAqGMViBjq0yJzXgZBvwVsc4Z55S8ziV1Q5HCJmAvjWUwrYB5MB+K10gf5wc8K7G 8H6q9gsgWXBMFo8wHhEpyvWJFoQOHKJlgi2mvm4s7cgttFlxwRRzeBDlm9Acxlv7QblJ VLWw== X-Gm-Message-State: AOAM532mW3XXOOJdsR221IFclX4/WGsFM601/9yLdYPqXGnuLuN3B0Rz 6lYHttzikHF3m7vtp9Oba9MlWJrmGyk7Ozg2 X-Google-Smtp-Source: ABdhPJwlzzVV8cezpp+lZPGV8sQxPh6PXMFrYXZQ+PP5GHbTqphHqDI++XqRe1knl34pzAPLMJAijg== X-Received: by 2002:a05:6830:1e52:: with SMTP id e18mr778510otj.135.1625158531033; Thu, 01 Jul 2021 09:55:31 -0700 (PDT) Received: from Ralphs-ZiffBook-Pro.local (ip72-204-153-233.no.no.cox.net. [72.204.153.233]) by smtp.gmail.com with ESMTPSA id v7sm60414ooj.46.2021.07.01.09.55.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 01 Jul 2021 09:55:30 -0700 (PDT) To: Larry Garfield , php internals References: <1ae611cb-b077-3db0-3237-c7ffa1c6e745@ralphschindler.com> <4776c6ba-7d0d-4383-bc2e-a172d9bf11e1@www.fastmail.com> Message-ID: <2572b7ee-c05a-0a5b-3a68-1a2882aa5588@ralphschindler.com> Date: Thu, 1 Jul 2021 11:55:29 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <4776c6ba-7d0d-4383-bc2e-a172d9bf11e1@www.fastmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [Proposal] call_user_func_map(): flexible named parameter mapping for call_user_func* From: ralph@ralphschindler.com (Ralph Schindler) > What I don't understand is what the issue with that is? > > You can already build up a named array and splat it into a function call to pass by name. It's a really neat trick. It doesn't need a dedicated function. > > About a month ago, I went through all of TYPO3 and removed all remaining call_user_func_array() calls in favor of just $function(...$args), because it does the same thing and is faster, and easier to read. Named arguments already work exactly that way, too. > > I don't see an advantage to adding a function that does what you can already do with less syntax: > > $args['foo'] = 5; > $args['beep'] = get_value_from_db(); > $args['narf'] = 'poink'; > > $callable(...$args); > > That works today in 8.0. We're good. The proposal focuses on the onus put on the author of the closure/function, not the caller. If your $callable is only interested in $foo enforced as an int and $narf enforced as a string, what does the signature and body of the subscribing $callable look like in this case? I am proposing that if a system is providing all of this for any registered callables: $args['foo'] = 5; $args['beep'] = get_value_from_db(); $args['narf'] = 'poink'; $args['boop'] = $boopObject; $args['isNoomable'] = false; But are only interested in value for 'foo' and 'narf', they can still get type hinting/checking/enforcement and the benefits of named mapping. Let's assume the following which has a little bit of context: // developer/consumer code $library = (new SomeLibrary) $library->registerCallback(function ($foo, $narf) {}); $library->call(); // some 3rd party library with a callback system in place class SomeLibrary { public function registerCallback(callable $callback) { $this->callback = $callback; } public function call() { if (!$this->callback) { return; } $args = []; $args['foo'] = 5; $args['beep'] = 'value from db'; $args['narf'] = 'poink'; $args['boop'] = new StdClass; $args['isNoomable'] = false; ($this->callback)(...$args); } } This will fail with message: PHP Fatal error: Uncaught Error: Unknown named parameter $beep in ... Currently, the consumer/developer would have to write: $library->registerCallback(function (...$args) { $foo = $args['foo']; $narf = $args['narf']; // ... }); // or $library->registerCallback(function ($foo, $beep, $narf, $boop, $isNoomable) { // do something with foo or narf }); Additionally, the library/framework is now precluded from adding new parameters since any consuming callables would also have to introduce these new parameters as it could be considered a BC break. The more I think about it though, the more sensible this would be as it allows the callback provider to opt into named parameter mapping destructuring: $library->registerCallback(function (...[string $foo, string $narf]) { // do something with $foo and $narf }); -ralph