Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112088 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 26435 invoked from network); 21 Oct 2020 10:58:30 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 21 Oct 2020 10:58:30 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AF4201804B3 for ; Wed, 21 Oct 2020 03:15:37 -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,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f174.google.com (mail-lj1-f174.google.com [209.85.208.174]) (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, 21 Oct 2020 03:15:37 -0700 (PDT) Received: by mail-lj1-f174.google.com with SMTP id a5so1948014ljj.11 for ; Wed, 21 Oct 2020 03:15:37 -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=mkpUJVdCURAnPbwfu5Xm8EaaCp7EmzPecbbJtmthe5o=; b=Gi2PmNTJZAxgtpt04tM6szxm4Ez6l8sHBiPlDii6t6U5wRDIY7nEjqhOZ64Y612JZt /rRQy+AdYE16V0J6vGEiTyDAj32yp13DgnTiti1n5hwceXaSjnxDyooKS4CbVvMy7ArA aNJw3pPxaGkLgqcEi2UAOSiw5tSwNEco8j5y3TOuf5TNfYWIF0MAcZetWidPbecHBjRI 4yIOsVflimgrp/H6WjVp/GyHZSwzIeax7BIVXAjO8YE4BYh+Pf0kpRDPpCz+/BFES8we S7/tS6BRu1Dbm3rmkohkfdDmTMaHQ0OvlymHIlFSPiAj12Wd5xY7OKANfti+MZCCGdOl 4tjA== 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=mkpUJVdCURAnPbwfu5Xm8EaaCp7EmzPecbbJtmthe5o=; b=dT0W4k2WM5qzZTR6lQgPhH6gANo52OmqOoMvha+3volAlmC2hTcHxGaZp/+C37HWg1 lpSLRCrWcHsjX9oVNHffjMSe+ptiVMc6Ga+McXzwoQ4hAzQHWENzL3eKtXHQSYUjHH5o MPVzgoQVsfESuRM8HCXZua+BrrrFaKbfAKFulsd5Q/4AdfnN2kWqRrXEo/0wmMJoK/V5 dWCc1RQj4LO/ADssUAXWBCBfOFH+Q6JAAjjD/RQWMDqGyF1f3xUvIAwKBLmvFsJMPQ1K ESCIKn3KFnCoMei5lfjunnLfiKoyxGxu9Mei87erO4FQxJhqnN3rPL1VjWncBu6Vxn0V korg== X-Gm-Message-State: AOAM530nG1+hfwLd0Ef0tU+2dsEG15qAlmliq41n2Y2ouMqQtA0IKFW6 CT5QcB4asn/lgN1OPINdDIMyH2TsobIqNPKROnA= X-Google-Smtp-Source: ABdhPJzM3AEGeOnmMLIXEJaGw3hl/gkA4nbvtSxEPZZqg6IH9CX+WrEAOgdBXeRFdrQwsWvsxx3pRftAjFND72P7CPk= X-Received: by 2002:a2e:a375:: with SMTP id i21mr1058108ljn.135.1603275333898; Wed, 21 Oct 2020 03:15:33 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 21 Oct 2020 12:15:17 +0200 Message-ID: To: tyson andre Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="000000000000c1167e05b22ba0ae" Subject: Re: [PHP-DEV] Idea: Warning about named parameters with a missing constructor? From: nikita.ppv@gmail.com (Nikita Popov) --000000000000c1167e05b22ba0ae Content-Type: text/plain; charset="UTF-8" On Wed, Oct 21, 2020 at 1:01 AM tyson andre wrote: > Hi internals, > > Currently, if there is no constructor, php handles it just like > `__construct(...$args) {}`, > both for positional and named parameters. Is there any interest in > changing that to be a deprecation warning if one or more parameters are > passed? > (or if 1 or more named parameters are passed) > > The default behavior of missing constructors would result in somewhat > surprising behavior > for `new $className(paramName: 123)` if this was used unexpectedly > (silently ignoring it) > (Desired behavior would be to deprecate/warn/throw) > > ``` > php > class A {} > php > var_export(new A(paramName: 123)); > A::__set_state(array( > )) > php > var_export(new stdClass(paramName: 123)); > (object) array( > ) > php > var_export(new stdClass(123)); > (object) array( > ) > ``` > > (I don't think https://wiki.php.net/rfc/named_params mentioned this > (the current behavior follows from handling of extra positional > parameters), > and this mistake is probably unlikely in practice, > but if it were to occur it would very likely be a bug) > > PHP already throws ArgumentCountError for passing too many parameters for > internal methods > so the default method not throwing is a surprise. I haven't yet looked > into how much code would break if this were changed. > > Aside: If a static analyzer such as Phan, Psalm, or PHPStan are set up, > those bugs could be detected easily, > so the impact seems low, but I wonder if this type of bug could cause > confusion (or feel unexpected) > for those who are more familiar with other languages than they are with > php. > > Thanks, > - Tyson > Thanks for bringing this up Tyson! There's two ways to interpret how this works: 1. We're calling an implicit constructor with signature __construct() {}, in which case unknown named args should not be accepted. 2. We're calling an implicit constructor with signature __construct(...$args) {}, in which case unknown named args should be accepted. I believe we should be following 1. here and have put up https://github.com/php/php-src/pull/6364 to fix this issue. (This actually happens to match what we were doing prior to PHP 8, I changed the signature of the pass function for unrelated reasons without considering this interaction.) As to the case of normal positional arguments, I agree that we should move towards making that an error as well, but this will need a deprecation phase, as it is long standing behavior. There's also the more general question of passing additional arguments to userland functions, but that one is a lot more controversial. Regards, Nikita --000000000000c1167e05b22ba0ae--